GameDesign 板


LINE

我测试的内容是拿这两项 第一部份是,拿 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







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灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP