javac速度_Java和C#运⾏速度对⽐:Java⽐C#快约3倍测试条件:
Java版本: Java 8,
.NET版本:v4.0, Release模式,针对x86平台优化
单线程模式。
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;
}
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;
DecimalFormat;
python转java代码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 = Time() - Time(); StringBuffer maxToshow=new StringBuffer(); maxToshow.append(max);
int count=0;
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, String());
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')
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论