简体   繁体   English

基本数据类型的性能与其包装器类的关系

[英]Performance of Primitive Data types VS their Wrapper class

I tried to measure execution time of primitive data types and their wrapper class for counting same number. 我试图测量原始数据类型及其包装器类的执行时间,以计算相同的数量。 I got that wrapper class is taking more time than primitive data types. 我知道包装器类比原始数据类型花费更多的时间。

Execution time of primitive t1=5 and Execution time of wrapper class t2= 31 in my following code. 在我的以下代码中,原始t1的执行时间为5,包装类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);

  }
}

Is there performance difference due to object creation of Wrapper class (Integer) or anything else ? 是否由于Wrapper类(整数)的对象创建或其他原因而导致性能差异?

You are getting essential things really wrong here. 您在这里得到的是必不可少的东西,实际上是错误的。

First of all, writing a good micro-benchmark goes way beyond what you are doing in your code; 首先,编写好的微基准测试远远超出了代码中的工作范围。 see here for some guidelines. 请参阅此处以获取一些指导。

Then: you have to understand the things you want to benchmark. 然后:您必须了解要进行基准测试的内容。 And sorry, you clearly do not. 抱歉,您显然没有。 Like here: 像这儿:

public static long wrapper(Integer count)
{
  long startTime = System.currentTimeMillis();
  for(int i=0;i<10000;i++)
    count++

Integer objects are immutable . 整数对象是不可变的 This code does not only "add" Integer objects, it creates one new Integer object per iteration. 此代码不仅“添加” Integer对象,而且还在每次迭代中创建一个新的Integer对象。

And of course: your code doesn't even use the computation result. 当然:您的代码甚至不使用计算结果。 If the JVM/JIT notices that, it might completely throw away that looping and adding construct. 如果JVM / JIT注意到这一点,它可能会完全放弃该循环并添加构造。 So your methods should at least return the final value of count; 因此,您的方法至少应返回count的最终值; and the caller should print that result. 呼叫者应打印该结果。

And to answer your specific question: of course using the reference type wrapper classes comes at certain cost. 并回答您的特定问题:当然,使用引用类型包装器类需要付出一定的代价。 When your program is dealing with (really) high numbers of elements to work on, then using a List of Integer does have considerable higher cost than using an array of int for example. 当您的程序要处理(确实)要处理的大量元素时,例如,使用整数列表确实比使用整数数组要高得多。 And when you don't pay attention and you are doing implicit autoboxing/unboxing in your code, that can really hurt . 而且,当您不注意并且在代码中执行隐式自动装箱/拆箱操作时,这确实很有害

But the real answer here is: you are focusing on the wrong thing. 真正的答案是:您专注于错误的事情。 You see, your first priority should be to do a great design , followed by a well-tested, robust, readable, maintainble implementation. 您会看到,您的首要任务应该是进行出色的设计 ,然后进行经过良好测试的,健壮的,可读的,可维护的实现。 Of course, you should not do outright stupid things and waste performance, but well, before you worry about performance, you better worry about your Java skills in general. 当然,你不应该做彻底的蠢事,浪费的表现,但好了, 不用担心性能,对一般的Java技能,你更好的担心了。

Long story short: focus on understanding java and on "how do I create a good design"; 长话短说:专注于理解Java和“如何创建好的设计”; and forget about "performance" for now. 暂时不要考虑“性能”。 You only have to deal with "performance" when it is a real issue in your applicaiton. 当“性能”在您的应用程序中是一个真正的问题时,您只需要处理它即可。 And then you do real profiling in order to identify the root cause and fix that. 然后进行真正的性能分析,以找出根本原因并加以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM