120941138报:凌乱了,java比c++快

同样的算法,同样的数据....在我的电脑上java耗时4秒左右,c++耗时15秒左右......贴上代码...大家分析下...理论上不应该有这么大差距的啊....彻底凌乱了....

更新:是编译设置的时候debug和release的原因,用release c++运行3秒左右.....话说我以前一直不知道release和debug还有执行效率的区别.....

更新:看了新的评论又凌乱了.....java开了-server我的还是4秒左右.....

c++的代码

01#include <iostream>
02#include <ctime>
03using namespace std;
04  
05class Work {
06    private:
07    chara,b;
08    inlinevoid set(intaa,int bb) {
09        a=aa;
10        b=bb;
11    }
12    inlinevoid put(charc) {
13        a = b;
14        b = c;
15    }
16    inlineint get() {
17        returna + b - 96;
18    }
19    public:
20    inlinechar result(charaa,char bb,int n) {
21        set(aa, bb);
22        inttemp = 0;
23        for(int i = n; i > 2; i--) {
24            temp = get();
25            if(temp > 9) {
26                put((char) (temp / 10 + 48));
27                i--;
28                if(i == 2)
29                    break;
30                put((char) (temp % 10 + 48));
31            }else {
32                put((char) (temp + 48));
33            }
34        }
35        returnb;
36    }
37};
38  
39int main() {
40    time_told_time;
41    Work work;
42    old_time =time(NULL);
43    charresult=work.result('1','4',999999999);
44    intall_time=time(NULL)-old_time;
45    cout<<"结果为:"<<result<<" 耗时:"<<all_time<<"秒"<<endl;
46    return0;
47}

[代码] java的代码

01package test;
02  
03public class Main {
04    publicstatic void main(String[] args) {
05        Work work =new Work();
06        longoldTime = System.currentTimeMillis();
07        charresult=work.result('1','4', 999999999);
08        longtime = System.currentTimeMillis() - oldTime;
09        System.out.println("结果为:"+result+" 耗时:"+time +"毫秒");
10    }
11}
12  
13class Work {
14    privatechar a, b;
15    privatevoid set(chara, char b) {
16        this.a = a;
17        this.b = b;
18    }
19    privatevoid put(charc) {
20        a = b;
21        b = c;
22    }
23    privateint get() {
24        returna + b - 96;
25    }
26    publicchar result(chara, char b, int n) {
27        this.set(a, b);
28        inttemp = 0;
29        for(int i = n; i > 2; i--) {
30            temp =this.get();
31            if(temp > 9) {
32                this.put((char) (temp /10 + 48));
33                i--;
34                if(i == 2)
35                    break;
36                this.put((char) (temp %10 + 48));
37            }else {
38                this.put((char) (temp +48));
39            }
40        }
41        returnthis.b;
42    }
43}

[图片] 未命名.jpg

[图片] 未命名.jpg

 

转载:开源中国
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值