merthsoft wrote:
Right, so why have the two specific cases and then the general? Why not have the simple optimization for repeating three characters? Or does the optimization break down after that?


Because you have to stop somewhere. They special cased one and two because repeating one or two characters is something they felt that happens frequently enough to justify special casing. Also, the optimization really isn't all that much, not worth spending any more code on that's for sure. They are basically doing the same thing that StringBuilder does anyway, they are just avoiding a couple of calls and some extra memory reads that would end up cached anyway.

Quote:
What about what Tari said about the comparison to zero? I googled around and saw that reasoning come up a little bit, but I'm not familiar enough with how all that would be implemented to know.


What Tari said doesn't matter for two reasons:
1) Compilers have long since been made smart enough to notice such obvious optimizations if there actually was any benefit (Tari is probably speaking from experience with Z80, not modern day x86). This is the same reason nobody unrolls loops anymore - no point.

2) This is in Java, you can't optimize it much even if you try. By the time it is warped into JVM and then JIT compiled who the hell knows what it ends up as?
Ah, ok. That all makes perfect sense, thanks!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement