Java和C#运行速度对比:Java比C#快约3倍
测试条件:
Java版本: Java 8,
.NET版本:v4.0, Release模式,针对x86平台优化
单线程模式。
测试1:(测试1的代码摘自http://blog.csdn.net/houjin_cn/article/details/5957432)
Java代码:
package javatest; public class temp
{
public static void main(String[] args)
{
long nn = System.nanoTime();
int times = 9999;
factorial(times);
nn = System.nanoTime() - nn;// 单位为ns(纳秒,即10的负9次方秒),
System.out.println((nn / 1000000) + " ms");
} private final static void factorial(int n)
{
int[] bs = new int[16384];
int top = 1;
bs[0] = 1;
for (int i = 2; i <= n; i++)
{
int w = 0;
for (int ic = 0; ic < top; ic++)
{
int tn = bs[ic];
tn = tn * i + w;
w = tn >>> 16;
bs[ic] = tn & 0xffff;
}
if (w != 0)
{
bs[top++] = w;
}
}
}
}
C#代码:
static void Main(string[] args)
{
long nn = System.DateTime.Now.Ticks;
factorial();
nn = System.DateTime.Now.Ticks - nn;// 单位为刻度数,1刻度数=10的负7次方秒
System.Console.Out.Write((nn / ) + " ms");
System.Console.Out.WriteLine();
Console.Read();
} static void factorial(int n)
{
int[] bs = new int[];
int top = ;
bs[] = ;
for (int i = ; i <= n; i++)
{
int w = ;
for (int ic = ; ic < top; ic++)
{
int tn = bs[ic];
tn = tn * i + w;
w = tn >> ;
bs[ic] = tn & 0xffff;
}
if (w != )
{
bs[top++] = w;
}
}
}
测试结果
Java:

C#:

结论1:Java速度是C#的2.8倍
测试2:同一个函数,执行两亿次。
Java代码(省略了打印):
package javatest; import java.text.DecimalFormat;
import java.util.Date; public class speedTest
{
public static void main(String[] args)
{
int i = 0;
int max = 200000000;//两亿次
Date start = new Date();
while (i++ < max)
{
transform();
}
Date end = new Date();
long diff = end.getTime() - start.getTime(); StringBuffer maxToshow=new StringBuffer();
maxToshow.append(max);
int count=0;
for(int a=1;a<String.valueOf(max).length();a++)
{
if(a%3==0)
{
count++;
maxToshow.insert(String.valueOf(max).length()-count*3, ",");
}
}
//String s = String.format("it takes %f seconds to run %s loops",
// diff / 1000.00, new DecimalFormat(",###").format(max));
String s = String.format("it takes %f seconds to run %s loops",
diff / 1000.00, maxToshow.toString());
System.out.println(s);
} public static void transform()
{
String str = "Pcybgle rfgq rsrmpgyj fyq npmzyzjw pcgldmpacb wmsp glrcpcqr gl sqgle Nwrfml - wms qfmsjb zc cyecp rm ynnjw Nwrfml rm qmjtgle wmsp pcyj-umpjb npmzjckq. Ufcpc qfmsjb wms em rm jcypl kmpc?";
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if (c >= 'a' && c <= 'z')
{
c += 2;
if (c > 'z')
{
c -= 26;
}
// System.out.print(c);
} else if (c >= 'A' && c <= 'Z')
{
c += 2;
if (c > 'Z')
{
c -= 26;
}
// System.out.print(c);
}
else
;// System.out.print(c);
}
}
}
C#代码(省略了打印):
static void Main(string[] args)
{
int i = ;
int max = ;// 两亿次
DateTime start = DateTime.Now;
while (i++ < max)
{
transform();
}
Console.WriteLine();
DateTime end = DateTime.Now;
Console.WriteLine("it takes {0} seconds to run {1} loops",
(end - start).TotalSeconds, max);
Console.Read();
} public static void transform()
{
String str = "Pcybgle rfgq rsrmpgyj fyq npmzyzjw pcgldmpacb wmsp glrcpcqr gl sqgle Nwrfml - wms qfmsjb zc cyecp rm ynnjw Nwrfml rm qmjtgle wmsp pcyj-umpjb npmzjckq. Ufcpc qfmsjb wms em rm jcypl kmpc?";
for (int i = ; i < str.Length; i++)
{
int c = str[i];
if (c >= 'a' && c <= 'z')
{
c += ;
if (c > 'z')
{
c -= ;
}
// Console.Write((char)c);
}
else if (c >= 'A' && c <= 'Z')
{
c += ;
if (c > 'Z')
{
c -= ;
}
// Console.Write((char)c);
}
else
;// Console.Write((char)c);
}
}
测试结果:
Java:

C#:

结论2:Java速度是C#的3.74倍:
Java和C#运行速度对比:Java比C#快约3倍的更多相关文章
- Java和Python运行速度对比
Java和Python运行速度对比:同一个函数运行一百万次,Java耗时0.577秒,Python耗时78秒--135倍的差距. 版本:Java 8,Python 2.7.10 Java测试代码: i ...
- 《Java疯狂讲义》(第3版)学习笔记 2 - Java语言的运行机制
内容 1.高级语言的运行机制 2.Java 语言的运行机制 1.高级语言的运行机制 高级语言主要分为编译型语言和解释型语言两类. 编译型语言是指使用专门的编译器.针对特定平台(操作系统)将高级语言源代 ...
- 2. Java程序的运行机制
一.完成一个Java程序的流程:编辑Java源代码→编译Java程序→运行Java程序 1. 在记事本中编写Java程序,然后保存为.java类型文件(Java源文件) 2. 使用javac命令将源文 ...
- [零基础学JAVA]Java SE基础部分-01. Java发展及JDK配置
转自:http://redking.blog.51cto.com/27212/114976 重点要会以下两个方面: 1. 抽象类与接口 2. API==>类集 这是两个最重要部分,这两个部分理解 ...
- 2020重新出发,JAVA语言,什么是JAVA?
@ 目录 什么是 java? JAVA三大体系 Java SE Java EE JavaME java的主要特性和优势 1. 面向对象 2. 平台无关性 3. 可移植性 4. 简单性 5. 解释执行 ...
- JVM学习(1)——通过实例总结Java虚拟机的运行机制
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: JVM的历史 JVM的运行流程简介 JVM的组成(基于 Java 7) JVM调优参数:-Xmx和-Xms ...
- Java虚拟机及运行时数据区
1.Java虚拟机的定义 Java虚拟机(Java Virtual Machine),简称JVM.当我们说起Java虚拟机时,可能指的是如下三种不同的东西: 抽象的虚拟机规范 规范的具体实现 一个运行 ...
- 如何利用 JConsole观察分析Java程序的运行,进行排错调优
原文链接:http://jiajun.iteye.com/blog/810150 一.JConsole是什么 从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能 ...
- 如何用javac 和java 编译运行整个Java工程 (转载)【转】在Linux下编译与执行Java程序
如何用javac 和java 编译运行整个Java工程 (转载) http://blog.csdn.net/huagong_adu/article/details/6929817 [转]在Linux ...
随机推荐
- jQuery应用之(三)jQuery链
从前文的实例中,我们按到jQuery语句可以链接在一起,这不仅可以缩短代码长度,而且很多时候可以实现特殊的效果. <script type="text/javascript" ...
- CentOS7 安装 Mono
官网参考:http://www.mono-project.com/docs/getting-started/install/linux/#centos-7-fedora-19-and-later-an ...
- "use strict"
"use strict";//严格模式 <!doctype html> <html> <head> <meta charset=" ...
- sql-char和varchar,nvarchar的区别
数据类型的比较 char表示的是固定长度,最长n个字 varchar表示的是实际长度的数据类型 比如:如果是char类型,当你输入字符小于长度时,后补空格:而是varchar类型时,则表示你输入字符的 ...
- 【HDU 4602】Partition
题意 给你一个数n,把它写成几个正整数相加的形式,即把n拆开成若干段,把所有可能的式子里正整数 k 出现的次数取模是多少. 分析 特判 k>=n 的情况. k<n时:问题相当于n个点排一行 ...
- org.springframework.beans.factory.annotation.Autowired(required=true)
Injection of autowired dependencies failed ERROR org.springframework.web.context.ContextLoader - Co ...
- 用VSCode写python的正确姿势
最近在学习python,之前一直用notepad++作为编辑器,偶然发现了VScode便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持markd ...
- android经典实战项目视频教程下载
注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...
- Yii查看执行的SQL
开户debug 修改配置文件 :protected/config/main.php, 'log' => array( 'class' => 'CLogRouter', ...
- 防止ajax请求重发
debounce ajax请求,防止用户点击过快造成重发 按钮disabled处理,显示loading,防止用户失去耐心,重复点击 表单提交也可以同样处理.