开发者

Which programing practice is faster? [closed]

开发者 https://www.devze.com 2023-03-23 20:06 出处:网络
开发者_如何学编程 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
开发者_如何学编程 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I created a small application in Java, since i am not java expert so my program is not much efficent (by efficency i mean less execution time i.e i want faster results). I would be thankful if some experienced person will answer my following questions:

Which one is more faster:

Question1

boolean b1;
b1=true;

or

boolean b1= true;

Question2

boolean b1;
boolean b2;

or

boolean b1, b2;

Question3

for (int index goes to some iterations)
{
MyCustomClass obj = new MyCustomClass ();
//Some other code goes here
}

or

MyCustomClass obj = new MyCustomClass ();
for (int index goes to some iterations)
{
obj.Reset();// Assume .Reset() have same codes as zero argument constructor has
//Some other code goes here
}

Thanks in advance


  1. Neither. This isn't even a micro-optimization.
  2. Neither...
  3. Depends on how much garbage is generated by Reset() vs creating a new object.


1) They're the same. They'll be compiled into the same byte code at least.

2) Same thing.

3) The second one is better, since it doesn't have a constructor in the loop it will take up less memory and be less work for the Garbage Collector.


Question 1 and question 2 produce identical byte code

For question 3, depends on your object, but generally do not create new objects in a loop unless necessary.


For Question 1 and Question 2, I would suspect that they would compile to the same bytecode. This can easily be tested by actually compiling two class files that differ only in this regard and look at the bytecode. I honestly don't see why they wouldn't be optimized into the same code, though, but I'm not an expert on the Java compiler. Even if it did matter, the difference would be so minute that it would end up not mattering in terms of an entire system's performance.

As for Question 3, resetting the object would probably be faster. Object creation is relatively expensive. Static analysis tools, such as FindBugs and PMD, actually look for object creation inside of a loop and flag it. Of course, it depends on exactly what Reset() is doing.

As with any "optimizations", though, be careful. First write the most readable code. Then, test the application. If performance is an issue, profile to find the bottlenecks and fix the least performant areas first.


In general, the compiler (for any language) knows how to optimize the result, no matter how you have coded. So , for the questions in the begining, I think it doesn't matter.

For the last one: If you create a new object inside the loop, it'll loose some time creating it: memory allocation, etc.

If you create it outside the loop, and them just call one method inside the loop, it should be faster, since it doesn't need to deal with all the memory allocation, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消