作者LaPass (LaPass)
看板GameDesign
标题Re: [请益] Html5手游潮流(?)
时间Sun Nov 1 23:04:13 2015
我测试的内容是拿这两项
第一部份是,拿 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://webptt.com/cn.aspx?n=bbs/GameDesign/M.1446390258.A.21C.html
※ 编辑: LaPass (114.38.75.222), 11/01/2015 23:09:01
1F:推 cowbaying: java万岁 11/01 23:17
※ 编辑: LaPass (114.38.75.222), 11/02/2015 00:02:21
2F:推 damody: c++要用vector来做啊你改用vector试试。 11/02 14:17
3F:推 Shinpachi: 搭配最近上市Rpg maker MV 应该是会有一波小风潮 11/02 16:40
4F:推 wix3000: JS真的要挡不住了 11/02 17:12
5F:推 chowleft: 有实验结果给推 11/02 19:02
6F:→ hoyunxian: 不过RMMV好像不少人有提到说很吃记忆体的问题 11/02 22:33
7F:推 holymars: 写个回圈让两个测试都跑100遍再统计时间会比较好 11/03 07:18
8F:→ Frostx: 推实验 11/04 11:25