• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

create object vs. casting list obj (performance)

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the performance diffrence of casting a List object to creating a new Object.

It seems to me that even though your using an objects refernce your still creating a new interface.

What is the diffrence between this:


and this:


in my particular application I know the index of the List so I will not need to slow this down with an iterator.

Thanks,
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm.
You can't create a new interface - pedantic speaking.
But talking about these things, we need to be pedantic.

When you cast an element from a collection, you don't create a new object - or a new instance, but get exactly that object, which was put into the collection (or added or inserted...).

That is in first place a question of semantic.


will give you an element from the list.
But

(you made a syntax-error at this point in your code)
will give you something.

For performance:
a) There is a well known saying from old Guru Knuth:
premature optimization is the root of all evil.
b) c) d) -> see a)

e) Constructor call's aren't gratis.
f) How many casts do you need per second?
It will depend on your machine, on the jvm, on the rest of the program.

g) Make some test.
If your machine isn't state of the art, 10 Million casts could be enough to show a small difference to a constructor-call.
But you have to make many tests, and run as well with identical values, as with different ones, to exclude circumstances influence your measures (foreign programs running in the background, consuming cpu, caches holding something in memory and so on).
And a solution performing well for 100 cases may perform lousy for 200.

But in real life, you can't choose.
If you could go without the list, why use a list at all?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic