Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ... performance  (Read 4643 times)
0 Members and 1 Guest are viewing this topic.
Offline fletchergames

Senior Devvie





« Posted 2008-04-16 17:54:23 »

What is the performance of methods using ... parameters as opposed to methods using a single element or an array as parameters?  In other words, how does code A compare to code B, performance-wise.

Code A:

1  
2  
3  
public void add(final E... aObjectToAdd) {
   //code here
} //end add


Code B:

1  
2  
3  
4  
5  
6  
7  
public void add(final E object) {
   //code here
} //end add

public void addAll(final E[] aObjectToAdd) {
   //code here
} //end addAll


It is my assumption that the ... method creates an array for the method even if you give it only one argument.  If such is the case, then I would assume that the ... method will always perform the same as the array method and worse than the single object method.

I've been using the ... because it's easier.  However, I might not want to use it in the utility classes I'm writing if it harms performance.

If I define the following 2 methods:

1  
2  
3  
4  
5  
6  
7  
public void add(final E object) {
   //code here
} //end add

public void add(final E... aObjectToAdd) {
   //code here
} //end add


Will it use the single object method for single objects and the other method for lists of objects?  If such is the case, I will probably do it that way.
Offline Linuxhippy

Senior Devvie


Medals: 1


Java games rock!


« Reply #1 - Posted 2008-04-17 14:58:27 »

Yes, unfourtunatly the varargs create an Object[] for now - maybe future runtime optimizations can decrease the cost of this operation.
If you write something _very_ sensitive to performance like a raytracer where a vararg-method is called millions of times, I would suggest not using it.

lg Clemens
Offline fletchergames

Senior Devvie





« Reply #2 - Posted 2008-04-17 15:06:47 »

I think I will just have add(Object) and addAll(Object...).  Since I'm using different method names, there's no possible conflict.

I'm writing generic utility classes, so I would like them to be as efficient as possible.  The add(Object) method isn't really necessary for completeness, but it would be faster than addAll(Object...) when only adding one Object.  The array creation might only be 10 or so instructions, but however many instructions it takes, it's unnecessary.

It's faster to use addAll(Object...) if you want to add multiple Objects because the utility class needs to reallocate an array when it runs out of space.  So I'll include both methods.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JAW

Senior Devvie


Medals: 2



« Reply #3 - Posted 2008-05-09 12:42:41 »

As far as I know the varargs is internally changed into an Array. When speaking about a perfomance issue, keep 2 things in mind:

- The method has to be called often in order to generate an issue on perfomance just because of the array parameter.
- The method has to be called a lot of times with only one parameter, where the simple version would be better.

Especially the second point brings me to:
If you have a method with varargs, it should usually be called with multiple arguments. If it is called like 90% with one argument and 10% with more than one, it could be strange programming.

Generally I would say this is another case for: Code first, optimize later. You can think a lot about the performance of the varargs method, but probably when you use it you will never measure a performance bottleneck caused by varargs. So I would suggest only to care about it, if you actually have a performance issure with varargs on your hand, otherwise feel free to use it.

-JAW
Offline fletchergames

Senior Devvie





« Reply #4 - Posted 2008-05-09 14:58:24 »

I've already written the code.

My program runs fine, so it's no big deal.  However, this is reusable code that I plan to eventually give out for free.  I want it to be fast.

Seriously, writing another method that doesn't use ... takes little time and isn't particularly bug prone. Optimizing this is relatively safe.  This isn't one of those cases where the optimization broke my program or took a really long time to implement.

Is it really worth profiling and so forth when I know the result will be an improvement (albeit a marginal one), and it's so easy to do?

The truth is that I only add one item to an array in about 70% of my code, but the add-one occurrences are far more likely to happen inside a loop.  Adding multiple items at once mostly occurs during initialization.

Enough of my code adds multiple items at once that I want the ... method, but I want to usually use the regular method.
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 841
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2008-05-09 15:10:51 »

If you have single-threaded code, you can do this: (to get rid of the array-construction)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
private static Object[] arr = new Object[1];
public void doSomething(Object obj)
{
   arr[0] = obj;
   this.doSomething(arr);
}

public void doSomething(Object... objs)
{
    // lots of stuff
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline fletchergames

Senior Devvie





« Reply #6 - Posted 2008-05-10 14:29:44 »

I don't have single-threaded code.  Now that everyone's getting multi-core processors, what would be the point?

Since the methods I was implementing are extremely simple, I might as well just have it implemented twice, once for an individual object and once for an array.
Offline Abuse

JGO Knight


Medals: 15


falling into the abyss of reality


« Reply #7 - Posted 2008-06-05 18:53:24 »

Will you be performing get operations upon this 'collection' (much?) more frequently than add?

If so, the unavoidable type casting necessary during retrieval will probably be a greater performance hinderance.

I'd forget performance, and make sure you write it 'properly' taking advantage of whatever language features you feel increase the quality of your code.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline fletchergames

Senior Devvie





« Reply #8 - Posted 2008-06-07 14:33:08 »

Though it's not clear from my posts to this thread, I did use generics.  There is no type casting.

In general, get operations will be performed more than add operations, but that's not always the case.  In fact, there was one case where get was enough more frequent than add that I started coding a CopyOnWriteArray, but the situation changed before I ever used the CopyOnWriteArrayCode.

I'm primarily concerned about performance in reusable library code.  I want it to run quickly because it will be reused in so many places.

The issue with ... is more than just creating the array.  It's also that I have to have a for loop for using the array.  Having two methods saves me alot of extra processor cycles because the class is used everywhere in my code.  It doesn't seem to reduce the readability of my code noticeably.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #9 - Posted 2008-06-09 15:03:26 »

If you have a good reason to add such a method for performance reasons (i.e. the method is typically called in performance critical code like inner loops), than I'd just go for it. It's not like your suggestion immediately makes the lib messy.

Generally I'm all for 'design clean code first, optimize later', but there are cases where it makes perfect sense to think about performance in advance too. It'd be such a waste having to refactor loads of code, just because the library method you use inherently doesn't perform well enough and the library method you would need to fix that wasn't there yet.

But then again, just test it first to see if it's at all likely (or even possible) to become a performance bottleneck.

Quote
Though it's not clear from my posts to this thread, I did use generics.  There is no type casting.
If you use generics, there is type casting. The compiler just hides it away from you.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fletchergames

Senior Devvie





« Reply #10 - Posted 2008-06-10 13:16:18 »

If you have a good reason to add such a method for performance reasons (i.e. the method is typically called in performance critical code like inner loops), than I'd just go for it. It's not like your suggestion immediately makes the lib messy.
It's not something that's going to make a big difference.  It mostly has an effect during the program's initialization, where add methods are called in some inner loops.

If it weren't for the ... parameters, I would have separate add and addAll methods anyways.  It just seems strange to create an array and then execute a for-loop (within the method) to add one element.

If you use generics, there is type casting. The compiler just hides it away from you.
I wasn't aware that generics did type casting internally, but I vastly prefer using generics anyways.  Either way, it was my understanding that neither generics nor type casting have any effect upon the bytecode, though I could be wrong about this.

Even if generics are slower, I would still use them because they produce compiler errors and/or warnings when I make the sorts of mistakes I often make.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #11 - Posted 2008-06-10 16:52:26 »

Quote
If it weren't for the ... parameters, I would have separate add and addAll methods anyways.  It just seems strange to create an array and then execute a for-loop (within the method) to add one element.

Yes, I know what you're saying. I personally don't like this kind of syntactic magic like '...' much (except maybe for some corner cases), so I would probably use add/addAll in the end. Even if the compiler would be smart enough to optimize this to something similar like your add/addAll.

Quote
I wasn't aware that generics did type casting internally, but I vastly prefer using generics anyways.  Either way, it was my understanding that neither generics nor type casting have any effect upon the bytecode, though I could be wrong about this.
It's my understanding that the compiler inserts type casts where needed.

Quote
Even if generics are slower, I would still use them because they produce compiler errors and/or warnings when I make the sorts of mistakes I often make.
Absolutely.
And I don't think generics are slower at all, it's just nicer syntax and better compile time checking.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (60 views)
2014-12-09 22:41:13

BurntPizza (95 views)
2014-12-08 04:46:31

JscottyBieshaar (55 views)
2014-12-05 12:39:02

SHC (71 views)
2014-12-03 16:27:13

CopyableCougar4 (73 views)
2014-11-29 21:32:03

toopeicgaming1999 (133 views)
2014-11-26 15:22:04

toopeicgaming1999 (124 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!