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

作者: LaPass (LaPass)   2015-11-01 23:04:13
我測試的內容是拿這兩項
第一部份是,拿 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
作者: cowbaying (是在靠北喔)   2015-11-01 23:17:00
java萬歲
作者: damody (天亮damody)   2015-11-02 14:17:00
c++要用vector來做啊你改用vector試試。
作者: Shinpachi (Shimura)   2015-11-02 16:40:00
搭配最近上市Rpg maker MV 應該是會有一波小風潮
作者: wix3000 (癢,好吃)   2015-11-02 17:12:00
JS真的要擋不住了
作者: chowleft (什麼....)   2015-11-02 19:02:00
有實驗結果給推
作者: hoyunxian (WildDagger)   2015-11-02 22:33:00
不過RMMV好像不少人有提到說很吃記憶體的問題
作者: holymars   2015-11-03 07:18:00
寫個迴圈讓兩個測試都跑100遍再統計時間會比較好
作者: Frostx (Naga)   2015-11-04 11:25:00
推實驗

Links booklink

Contact Us: admin [ a t ] ucptt.com