原始数据类型与其包装器的性能对比 class
Performance of Primitive Data types VS their Wrapper class
我试图测量原始数据类型及其包装器的执行时间 class 以计算相同的数字。我知道包装 class 比原始数据类型花费更多的时间。
原语的执行时间 t1=5 和包装器的执行时间 class t2= 31 在我的以下代码中。
import java.io.*;
import java.util.*;
public class Performance
{
public static long primitive(int count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++;
System.out.println(count);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
return elapsedTime;
}
public static long wrapper(Integer count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++;
System.out.println(count);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
return elapsedTime;
}
public static void main(String args[])
{
Integer c = new Integer(0);
long t2=Performance.wrapper(c);
int count=0;
long t1=Performance.primitive(count);
System.out.println("t1="+t1+"t2="+t2);
}
}
Wrapper class(整数)或其他对象的对象创建是否会导致性能差异?
你在这里把重要的事情弄错了。
首先,编写一个好的微基准测试远远超出了您在代码中所做的;请参阅 here 了解一些指南。
然后:您必须了解要进行基准测试的内容。对不起,你显然没有。喜欢这里:
public static long wrapper(Integer count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++
整数对象是不可变的。此代码不仅 "add" Integer 对象,而且每次迭代 创建 一个新的 Integer 对象。
当然:您的代码甚至不使用计算结果。如果 JVM/JIT 注意到这一点,它可能 完全 丢弃该循环和添加构造。所以你的方法应该至少 return 计数的最终值;并且调用者应该打印该结果。
并回答您的具体问题:当然,使用引用类型包装器 类 需要付出一定的代价。当您的程序处理(真的)大量要处理的元素时,使用整数列表 确实比使用 int 数组 具有更高的成本。而当你不注意,你在代码中做隐式 autoboxing/unboxing 时,那真的可以 hurt。
但是真正的答案是:你关注的是错误的事情。你看,你的第一优先事项应该是做一个伟大的设计,然后是一个经过良好测试的、健壮的、可读的、可维护的实现。当然,你不应该做完全愚蠢的事情和浪费性能,但是好吧,在你担心性能之前,你最好担心你的Java 技能一般。
长话短说:专注于理解 java 和 "how do I create a good design"; 暂时忘记"performance"。你只需要处理 "performance" 当它是你的应用程序中的 真正的 问题。然后您进行 真实 分析以确定根本原因并加以解决。
我试图测量原始数据类型及其包装器的执行时间 class 以计算相同的数字。我知道包装 class 比原始数据类型花费更多的时间。
原语的执行时间 t1=5 和包装器的执行时间 class t2= 31 在我的以下代码中。
import java.io.*;
import java.util.*;
public class Performance
{
public static long primitive(int count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++;
System.out.println(count);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
return elapsedTime;
}
public static long wrapper(Integer count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++;
System.out.println(count);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
return elapsedTime;
}
public static void main(String args[])
{
Integer c = new Integer(0);
long t2=Performance.wrapper(c);
int count=0;
long t1=Performance.primitive(count);
System.out.println("t1="+t1+"t2="+t2);
}
}
Wrapper class(整数)或其他对象的对象创建是否会导致性能差异?
你在这里把重要的事情弄错了。
首先,编写一个好的微基准测试远远超出了您在代码中所做的;请参阅 here 了解一些指南。
然后:您必须了解要进行基准测试的内容。对不起,你显然没有。喜欢这里:
public static long wrapper(Integer count)
{
long startTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
count++
整数对象是不可变的。此代码不仅 "add" Integer 对象,而且每次迭代 创建 一个新的 Integer 对象。
当然:您的代码甚至不使用计算结果。如果 JVM/JIT 注意到这一点,它可能 完全 丢弃该循环和添加构造。所以你的方法应该至少 return 计数的最终值;并且调用者应该打印该结果。
并回答您的具体问题:当然,使用引用类型包装器 类 需要付出一定的代价。当您的程序处理(真的)大量要处理的元素时,使用整数列表 确实比使用 int 数组 具有更高的成本。而当你不注意,你在代码中做隐式 autoboxing/unboxing 时,那真的可以 hurt。
但是真正的答案是:你关注的是错误的事情。你看,你的第一优先事项应该是做一个伟大的设计,然后是一个经过良好测试的、健壮的、可读的、可维护的实现。当然,你不应该做完全愚蠢的事情和浪费性能,但是好吧,在你担心性能之前,你最好担心你的Java 技能一般。
长话短说:专注于理解 java 和 "how do I create a good design"; 暂时忘记"performance"。你只需要处理 "performance" 当它是你的应用程序中的 真正的 问题。然后您进行 真实 分析以确定根本原因并加以解决。