一個基礎而奇怪的問題:演算法運行加法、乘法、除法效能無差別?,加法除法

來源:互聯網
上載者:User

一個基礎而奇怪的問題:演算法運行加法、乘法、除法效能無差別?,加法除法

一個基礎而奇怪的問題:演算法運行加法、乘法、除法效能無差別? 電腦原理分析認為:加法、乘法和除法的計算效能依次降低,但降低到什麼程度?

編寫C程式用30次百萬資料計算來測試時間差異性,代碼如下:

#include <stdio.h>#include <stdlib.h>#include <time.h>#define N 1000000void add(float x[], long n) {float sum = 0;for(long i = 0; i < n; i++)sum += x[i];}void prod(float x[], long n) {float sum = 1;for(long i = 0; i < n; i++)sum *= (x[i]);}void div(float x[], long n) { for(long i = 0; i < n; i++) {x[i] /= 3.0;}}int main(){float x[N];clock_t t1 = clock();for(int i = 0; i < 50; i++)add(x, N);clock_t t2 = clock();printf("百萬資料加法用時:%f 秒\n", (double)(t2 - t1)/ CLOCKS_PER_SEC);t1 = clock();for(int i = 0; i < 50; i++)prod(x, N);t2 = clock();printf("百萬資料乘法用時:%f 秒\n", (double)(t2 - t1)/ CLOCKS_PER_SEC);t1 = clock();for(int i = 0; i < 50; i++)div(x, N);t2 = clock();printf("百萬資料除法用時:%f 秒\n", (double)(t2 - t1)/ CLOCKS_PER_SEC);return 0;}

結果如下:

百萬資料加法用時:0.157051 秒
百萬資料乘法用時:0.184712 秒
百萬資料除法用時:0.161014 秒
-----------------------------------------
百萬資料加法用時:0.156099 秒
百萬資料乘法用時:0.184023 秒
百萬資料除法用時:0.159588 秒


說明了什麼問題呢?

是否應該說演算法中基本運算已無差別?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.