Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

casting reductions

 
Marilyn de Queiroz
Sheriff
Posts: 9079
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>
Let's push on and try eliminating the unnecessary String casts. This is done by simply holding the first casted object in a variable of the appropriate type. For example:
for(int i = 0; i < max; i++)
{
if( ( ((String) collection.get(i)).indexOf("ie") != -1 )
| | ( ((String) collection.get(i)).indexOf("xy") != -1 )
| | ( ((String) collection.get(i)).indexOf("pq") != -1 ) )
becomes
String s;
for(int i = 0; i < max; i++)
{
if( ( (s = (String) collection.get(i)).indexOf("ie") != -1 )
| | ( s.indexOf("xy") != -1 )
| | ( s.indexOf("pq") != -1 ) )
</pre>
How do you know that the difference in performance is only due to the reduction in casting? Aren't you also getting rid of a method call at the same time?
Marilyn
 
Jack Shirazi
Author
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are completely right. Until you just pointed it out I didn't notice that I had combined the two optimizations. This is quite common - it's too easy to combine optimizations without testing each separately.
Both optimizations should have independently worked to have slightly speeded up the test.
 
bacon. tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!