看板 C_Sharp 關於我們 聯絡資訊
: 另外,如果要算的次方數很大的話 : (應該到五次或是六次就應該呼叫了..) : 建議呼叫 Math.Pow 會快上很多.... : 裡面用的算法比你自己寫的連乘法好很多.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 這點跟我認知很不一樣 我沒看過Math.Pow()的source所以不敢說它演算法好不好 我只能作實驗來觀察 source如下 public partial class Form1 : Form { [DllImport("kernel32.dll")] extern static short QueryPerformanceCounter(ref long x); [DllImport("kernel32.dll")] extern static short QueryPerformanceFrequency(ref long x); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { long ctr1 = 0, ctr2 = 0, freq = 0; double sum = 0; double a = 0; Random myrand = new Random(); if (QueryPerformanceCounter(ref ctr1)!=0) // Begin timing. { for (int i=0; i<1000000000; i++) // Code being timed. { a = myrand.NextDouble() * 10; //Generate Random number //3 options sum = 6 * (a * a * a) + (5 * (a * a)) + (3 * a) - 1; //sum = 6*Math.Pow(a,3) + 5*Math.Pow(a,2)+(3*a) - 1; //sum = a * (a * (a * (+6) + 5) + 3) - 1; } QueryPerformanceCounter(ref ctr2); // Finish timing. this.richTextBox1.AppendText("Start Value: " + ctr1 + "\n"); this.richTextBox1.AppendText("End Value: " + ctr2+"\n"); QueryPerformanceFrequency(ref freq); this.richTextBox1.AppendText("QueryPerformanceCounter minimum resolution: 1/" + freq + " seconds."+"\n"); this.richTextBox1.AppendText("100 Increment time: " + (ctr2 - ctr1) * 1.0 / freq + " seconds."+"\n"); } else this.richTextBox1.AppendText("High-resolution counter not supported."+"\n"); } } Result1: sum = 6 * (a * a * a) + (5 * (a * a)) + (3 * a) - 1; Start Value: 757143142739 End Value: 757167788963 QueryPerformanceCounter minimum resolution: 1/14318180 seconds. 100 Increment time: 1.72132379953318 seconds. Result2: sum = 6*Math.Pow(a,3) + 5*Math.Pow(a,2)+(3*a) - 1; Start Value: 758355648048 End Value: 758755628202 QueryPerformanceCounter minimum resolution: 1/14318180 seconds. 100 Increment time: 27.9351254139842 seconds. Result3: sum = a * (a * (a * (+6) + 5) + 3) - 1; Start Value: 759758768779 End Value: 759783404913 QueryPerformanceCounter minimum resolution: 1/14318180 seconds. 100 Increment time: 1.720619101031 seconds. 再來換成冪次更高的 Result1: sum = 9 * (a * a * a * a * a * a) + 8 * (a * a * a * a * a) + 7 * (a * a * a * a) + 6 * (a * a * a) + (5 * (a * a)) + (3 * a) - 1; Start Value: 766926901793 End Value: 766951596182 QueryPerformanceCounter minimum resolution: 1/14318180 seconds. 100 Increment time: 1.72468770472225 seconds. Result2: sum = 9 * Math.Pow(a, 6) + 8 * Math.Pow(a, 5) + 7 * Math.Pow(a, 4) + 6 * Math.Pow(a, 3) + 5 * Math.Pow(a, 2) + (3 * a) - 1; Start Value: 769278944190 End Value: 770282636993 QueryPerformanceCounter minimum resolution: 1/14318180 seconds. 100 Increment time: 70.0991887935478 seconds. Result3: sum = a * (a * (a * ( a * (a * (a * (+9) +8) +7 ) +6) + 5) + 3) - 1; Start Value: 774103750407 End Value: 774128401956 QueryPerformanceCounter minimum resolution: 1/14318180 seconds. 100 Increment time: 1.72169570434231 seconds. 這實驗其實很粗糙 但是可以看的出有非常明顯的差異 由此看來 第一種寫法跟第三種寫法幾乎是沒有差別的 也許是.net compiler會自己最佳化 但是第二種(使用Math.Pow()) 很明顯的在執行時間上有段落差 冪次越大差距就越大 所以我的認知是要求效率的時候用連乘法會比Math.Pow()來的好 不過 也許是我實驗的方式有誤 有錯的話麻煩請各位賢達指正 謝謝 :) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.133.110.47
yoco315:你是對的.. 我弄錯了 :) 05/03 11:08
GreatShot:因為我吃過虧 所以印象深刻 :) 05/03 13:08
cole945:可能Math.Pow的overhead主要來自於function call? 05/03 13:15
cole945:我在我的電腦上測, Math.Pow(a,x) x在300以內,都是大約固 05/03 13:15
cole945:定在0.22ms左右, 可是a*a*a..卻很雖然連乘數增加@.@a 05/03 13:16
cole945: (我最後一句話的 "雖然" 是要打 "隨著" |||| ) 05/03 13:18
GreatShot:我也不知道為何 我執行Math.Pow(a,100)遠比連乘100次慢 05/03 13:41
GreatShot:我用的OS是Vista 64, 不知換個OS跑會不會有不同的結果 05/03 13:42
birdychang:QueryPerformanceCounter跟Environment.TickCount 05/03 19:38
birdychang:有不同嗎?? 05/03 19:39
birdychang:我是都用Environment.TickCount算時間耶~ 05/03 19:39
GreatShot:樓上~精確度不同~~ 05/04 06:58
GreatShot:精確度 QPC > TickCount > Timer 05/04 06:59
chhuang:我倒是都用 StopWatch 來測試時間... 05/04 11:12
GreatShot:我記得stopwatch就是用QPC來寫的 05/04 11:22
GreatShot:so其實stopwatch應該是最好用的 XD 05/04 11:25
Epimenides:剛試用了stopwatch 好用 05/05 18:14