Java、C#速度大比拼

  一直想知道Java和C#到底哪个跑得更快一些,刚好最近写了一个f(n) = n的C#程序,于是顺手改成Java看看二者在速度上的差别。关于f(n) = n的题目请参看文章: f(n) = n的优化(二)

  一、Java和C#速度大比拼第一回合
    把 f(n) = n的优化(二) 文章中的C#代码照搬不变的转换成Java代:
ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif * Main.java
InBlock.gif *
InBlock.gif * Created on 2006年9月13日, 下午2:09
InBlock.gif *
ExpandedBlockEnd.gif 
*/

None.gif
None.gif
package  test;
None.gif
None.gif
import  javax.crypto.Mac;
None.gif
import  java.lang. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif *
InBlock.gif * 
@author jailu
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  Main  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//** Creates a new instance of Main */
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Main() dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 
@param args the command line arguments
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
long start = System.currentTimeMillis();    //程序开始时间
InBlock.gif

InBlock.gif        
int N = 0;  //自然数N
InBlock.gif
        boolean bl = true;
InBlock.gif        
int intCount = 0;   //1的总个数
InBlock.gif
        int intTemp = 0;    //当前数字含有1的个数
InBlock.gif
        int a = 0,b = 0;
InBlock.gif
InBlock.gif        System.out.println(
"满足条件f(n)=n的n值列表:");
InBlock.gif
InBlock.gif        
//核心代码
InBlock.gif
        while(bl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (N % 10 == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intCount
++;
InBlock.gif                intCount 
+= intTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (N % 10 == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intTemp 
= 0;    //统计1的个数
InBlock.gif

InBlock.gif                b 
= N;
InBlock.gif                
while (b > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    a 
= b % 10;
InBlock.gif                    
if (a == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        intTemp
++;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    b 
= (b - a) / 10;
ExpandedSubBlockEnd.gif                }

InBlock.gif                intCount 
+= intTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (N % 10 == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intCount 
+= intTemp;
InBlock.gif                
if ((intCount >= N) && (intCount <= (N + 8)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    N 
+= 7;
InBlock.gif                    intCount 
+= (7 * intTemp);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intCount 
+= intTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (intCount == N)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.out.println(N 
+ "\t耗时\t" + (System.currentTimeMillis()
InBlock.gif
- start) + "ms");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (N >= 1111111111)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bl 
= false;
InBlock.gif                System.out.println(
"\n程序结束!");
InBlock.gif                System.out.println(
"共耗时" + (System.currentTimeMillis() - star
InBlock.gift));
ExpandedSubBlockEnd.gif            }

InBlock.gif            N 
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
//        System.out.println("\n程序结束!");
InBlock.gif
//        System.out.println("共耗时" + (System.currentTimeMillis() - start));
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

部分结果如下:
535199986       耗时    35078ms
535199987       耗时    35078ms
535199988       耗时    35078ms
535199989       耗时    35078ms
535199990       耗时    35078ms
535200000       耗时    35078ms
535200001       耗时    35078ms
1111111110      耗时    74313ms

程序结束!
共耗时74313

  由此可见Java程序运行时间约为74s,而C#程序的运行速度约为80s,此回合Java胜出。

  二、Java和C#速度大比拼第二回合:同时修改Java和C#代码(while(N <= 1111111110)),下面列出Java代码,C#代码与之类似:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif * Main.java
InBlock.gif *
InBlock.gif * Created on 2006年9月13日, 下午2:09
InBlock.gif *
ExpandedBlockEnd.gif 
*/
None.gif
None.gif
package  test;
None.gif
None.gif
import  javax.crypto.Mac;
None.gif
import  java.lang. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif *
InBlock.gif * 
@author jailu
ExpandedBlockEnd.gif 
*/
ExpandedBlockStart.gifContractedBlock.gif
public   class  Main  dot.gif {
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//** Creates a new instance of Main */
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Main() dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 
@param args the command line arguments
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
long start = System.currentTimeMillis();    //程序开始时间
InBlock.gif
        
InBlock.gif        
int N = 0;  //自然数N
InBlock.gif
        int intCount = 0;   //1的总个数
InBlock.gif
        int intTemp = 0;    //当前数字含有1的个数
InBlock.gif
        int a = 0,b = 0;
InBlock.gif        
InBlock.gif        System.out.println(
"满足条件f(n)=n的n值列表:");
InBlock.gif        
InBlock.gif        
//核心代码
InBlock.gif
        while(N <= 1111111110)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (N % 10 == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intCount
++;
InBlock.gif                intCount 
+= intTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (N % 10 == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intTemp 
= 0;    //统计1的个数
InBlock.gif

InBlock.gif                b 
= N;
InBlock.gif                
while (b > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    a 
= b % 10;
InBlock.gif                    
if (a == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        intTemp
++;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    b 
= (b - a) / 10;
ExpandedSubBlockEnd.gif                }

InBlock.gif                intCount 
+= intTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (N % 10 == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intCount 
+= intTemp;
InBlock.gif                
if ((intCount >= N) && (intCount <= (N + 8)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    N 
+= 7;
InBlock.gif                    intCount 
+= (7 * intTemp);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                intCount 
+= intTemp;
ExpandedSubBlockEnd.gif            }

InBlock.gif        
InBlock.gif            
if (intCount == N)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.out.println(N 
+ "\t耗时\t" + (System.currentTimeMillis() - start) + "ms");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            N 
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        System.out.println(
"\n程序结束!");
InBlock.gif        System.out.println(
"共耗时" + (System.currentTimeMillis() - start));
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

部分结果如下:
535199986       耗时    34828ms
535199987       耗时    34828ms
535199988       耗时    34828ms
535199989       耗时    34828ms
535199990       耗时    34828ms
535200000       耗时    34843ms
535200001       耗时    34843ms
1111111110      耗时    73656ms

程序结束!
共耗时73656

而C#程序的运行结果如下:
535199988
Total Time:     00:00:39.2187500

535199989
Total Time:     00:00:39.2187500

535199990
Total Time:     00:00:39.2187500

535200000
Total Time:     00:00:39.2187500

535200001
Total Time:     00:00:39.2187500

1111111110
Total Time:     00:01:23.1250000


Start Time:     2006-9-13 16:02:38
End Time:       2006-9-13 16:04:01
Total Time:     00:01:23.1250000
运行结束!

  由结果可以看出:
  1、第二回合中的Java程序比第一回合中的Java程序快了0.7s左右;
  2、第二回合中的C#程序非但没比第一回合中的C#程序快,反而慢了3s多,汗个,搞不懂是怎么回事,有兴趣的朋友可以自己试下!
  3、第二回合大比拼中,Java胜出!

  经此两回合的较量,不难发现:Java的运行速度略快于C#的运行速度!

转载于:https://www.cnblogs.com/jailu/archive/2006/09/13/503265.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值