C_Sharp 板


LINE

: 另外,如果要算的次方數很大的話 : (應該到五次或是六次就應該呼叫了..) : 建議呼叫 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
1F:推 yoco315:你是對的.. 我弄錯了 :) 05/03 11:08
2F:推 GreatShot:因為我吃過虧 所以印象深刻 :) 05/03 13:08
3F:推 cole945:可能Math.Pow的overhead主要來自於function call? 05/03 13:15
4F:→ cole945:我在我的電腦上測, Math.Pow(a,x) x在300以內,都是大約固 05/03 13:15
5F:→ cole945:定在0.22ms左右, 可是a*a*a..卻很雖然連乘數增加@.@a 05/03 13:16
6F:→ cole945: (我最後一句話的 "雖然" 是要打 "隨著" |||| ) 05/03 13:18
7F:推 GreatShot:我也不知道為何 我執行Math.Pow(a,100)遠比連乘100次慢 05/03 13:41
8F:→ GreatShot:我用的OS是Vista 64, 不知換個OS跑會不會有不同的結果 05/03 13:42
9F:推 birdychang:QueryPerformanceCounter跟Environment.TickCount 05/03 19:38
10F:→ birdychang:有不同嗎?? 05/03 19:39
11F:→ birdychang:我是都用Environment.TickCount算時間耶~ 05/03 19:39
12F:推 GreatShot:樓上~精確度不同~~ 05/04 06:58
13F:→ GreatShot:精確度 QPC > TickCount > Timer 05/04 06:59
14F:推 chhuang:我倒是都用 StopWatch 來測試時間... 05/04 11:12
15F:推 GreatShot:我記得stopwatch就是用QPC來寫的 05/04 11:22
16F:→ GreatShot:so其實stopwatch應該是最好用的 XD 05/04 11:25
17F:推 Epimenides:剛試用了stopwatch 好用 05/05 18:14







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP