Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.arraycopy VS toArray()

 
Charith Fernando
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i have a requirement where i get a set of id's and i have to search for the results and make an array out of the results...



1) one way to do this is by adding the results into a List and then at the end convert the list into an array by using the toArray() method.

2) the other way is to define the array to be the size of the ids array and then add the results into it and remove the unused elements of the result array at the end by doing an arraycopy...

which method is more efficient?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't seem to have anything to do with SCJP, so let's move it to Java in General (Intermediate)...
[ October 28, 2006: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like the two techniques are pretty close. The first seems a little more natural to me, probably takes a little less code. Having to "remove" the unused array slots in the second technique seems like you're giving yourself an extra opportunity to make a silly coding mistake along the way - why bother, when ArrayList already takes care of such details?
 
Charith Fernando
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so does that mean in performance wise both are same? as in you dont have a performance hit in either way?

would like to propose to open up a seperate section for performance tuning discussions... because we have to promote the most efficient way to do certain processes..
Thank you,
 
Charith Fernando
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry... i think there's one thread available for that under the name "Performance"
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charith,

the very first rule to learn about performance optimization is that you should only optimize code that you have identified to be a performance bottleneck. Everything else will just take away time from caring about the important aspects of development.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic