Re: [請益] Html5手遊潮流(?)

看板GameDesign (遊戲設計)作者 (LaPass)時間9年前 (2015/11/01 23:04), 9年前編輯推噓6(602)
留言8則, 8人參與, 最新討論串2/3 (看更多)
我測試的內容是拿這兩項 第一部份是,拿 1 相加到 10000000 出來的答案是 49999995000000 ,這個要考慮溢位問題 第二部份是,拿字串 1 相黏到 10000000 出來的結果會是 012345678910111213141516..... 以下是測試結果: node.js count 1-10000000 need time(22) add str 1-10000000 need time(3035) chrome count 1-10000000 need time(27) add str 1-10000000 need time(2072) 瀏覽器跑得比node.js還快真是嚇到我了..... JAVA count 1-10000000 needTime(11) str+= 1-10000000 needTime(超過一分鐘) add sb 1-10000000 needTime(659) java的字串相黏有兩種方式 一種是 String str=""; for (int i=0;i<10000000;i++) num+=i; 這種寫法的運作機制是,宣告一個新字串裝相加後的字串 另一種是 StringBuffer sb=new StringBuffer(); for (int i=0;i<10000000;i++) sb.append(i); 這種寫法的運作機制是,宣告一塊緩衝區,把字元填入。 java的官方建議是,如果用到大量字串操作,就用StringBuffer 順帶一提,js在有些實作中,字串是用linked list C++ count 1-10000000 need Time 30 add str 1-10000000 need Time 677 我搞不懂為什麼c++算的會比js或是java還慢..... Ruby count for 1-10000000 need Time(633) count times 1-10000000 need Time(606) += times 1-10000000 need Time(超過一分鐘) # times 1-10000000 need Time(超過一分鐘) << times 1-10000000 need Time(2434) Ruby的迴圈有兩種形式 一種是傳統的 for n in 0..100000 end 另一種是 10000000.times do |i| end s黏接字串的方式有三種 一種是 str+=str2 或是 str="#{str}#{str2}" 另一種是 str << str2 以下是測試用的程式碼 JS in web https://jsfiddle.net/psw414xx/ JS in node.js // init loop for (var i = 0; i < 10000000; i++) ; var start,end; var num=0; start=new Date(); for (var i = 0; i < 10000000; i++) num+=i; end=new Date(); console.log("count 1-10000000 need time("+(end.getTime()-start.getTime())+")"); console.log("num("+num+")"); var str=""; start=new Date(); for (var i = 0; i < 10000000; i++) str+=i; end=new Date(); console.log("add str 1-10000000 need time("+(end.getTime()-start.getTime())+")"); JAVA public class Test { public static void main(String[] args){ long start,end; long num=0; start=System.currentTimeMillis(); for (int i=0;i<10000000;i++) num+=i; end=System.currentTimeMillis(); System.out.println("count 1-10000000 needTime("+(end-start)+")"); System.out.println("num("+(num)+")"); //String str=""; //start=System.currentTimeMillis(); //for (int i=0;i<10000000;i++) str+=i; //end=System.currentTimeMillis(); System.out.println("str+= 1-10000000 needTime(超過一分鐘)"); StringBuffer sb=new StringBuffer(); start=System.currentTimeMillis(); for (int i=0;i<10000000;i++) sb.append(i); end=System.currentTimeMillis(); System.out.println("add sb 1-10000000 needTime("+(end-start)+")"); } } C++ #include <inttypes.h> #include <stdio.h> #include <sys/time.h> #include <string.h> #include <sstream> #include <iostream> using namespace std; int main(int argc, char** argv) { for (int i = 0; i < 10000000; ++i); struct timeval start,end; long long ts,te; long long num=0; gettimeofday(&start, NULL); for (int i = 0; i < 10000000; i++)num+=i; gettimeofday(&end, NULL); ts = (start.tv_sec) * 1000 + (start.tv_usec) / 1000 ; te = (end.tv_sec) * 1000 + (end.tv_usec) / 1000 ; printf("count 1-10000000 need Time %llu\n", te-ts); stringstream ss; gettimeofday(&start, NULL); for (int i = 0; i < 10000000; ++i) ss << i; gettimeofday(&end, NULL); ts = (start.tv_sec) * 1000 + (start.tv_usec) / 1000 ; te = (end.tv_sec) * 1000 + (end.tv_usec) / 1000 ; printf("add str 1-10000000 need Time %llu\n", te-ts); } Ruby http://ideone.com/RkpE62 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.38.75.222 ※ 文章網址: https://www.ptt.cc/bbs/GameDesign/M.1446390258.A.21C.html ※ 編輯: LaPass (114.38.75.222), 11/01/2015 23:09:01

11/01 23:17, , 1F
java萬歲
11/01 23:17, 1F
※ 編輯: LaPass (114.38.75.222), 11/02/2015 00:02:21

11/02 14:17, , 2F
c++要用vector來做啊你改用vector試試。
11/02 14:17, 2F

11/02 16:40, , 3F
搭配最近上市Rpg maker MV 應該是會有一波小風潮
11/02 16:40, 3F

11/02 17:12, , 4F
JS真的要擋不住了
11/02 17:12, 4F

11/02 19:02, , 5F
有實驗結果給推
11/02 19:02, 5F

11/02 22:33, , 6F
不過RMMV好像不少人有提到說很吃記憶體的問題
11/02 22:33, 6F

11/03 07:18, , 7F
寫個迴圈讓兩個測試都跑100遍再統計時間會比較好
11/03 07:18, 7F

11/04 11:25, , 8F
推實驗
11/04 11:25, 8F
文章代碼(AID): #1MDYdo8S (GameDesign)
文章代碼(AID): #1MDYdo8S (GameDesign)