File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is faster?

 
Remus Stratulat
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a critical operation and I need to know which is faster:

or

Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not time them both and see? It shouldn't be difficult. Put the method inside a loop so it's called many times, and use System.currentTimeMillis() to find the time before and after executing the loop. While you're at it, try this one as well:

Who knows, a smart compiler may even compile the switch statement so it's exactly equivalent to the above statement.
[ April 02, 2002: Message edited by: Jim Yingst ]
 
Remus Stratulat
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I just did that and the first version is about 4 time faster than the second.
About your solution: it can't be applied because in fact I'm not searching a contiguous interval.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about using a bit mask? I won't go into the details but you might be able to define the characters you matching against as a binary 'mask' such that when you do an XOR against the test value it will result in 0 or 1. Just a thought.
Sean
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remus- it sure looks contiguous. Does this mean the actual problem is different from what you posted?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a slight tangent, using the "hard-coded" first method isn't necessarily a bad thing, especially if you can write code that generates the code for you!
This is called generative programming. I sat in on a tech session on it at JavaOne. Basically, there are certain types of problems that lend themselves to this kind of thing. The idea is that you have source code that creates compilable java source code dynamically, that you can then compile, and get the efficiencies of using hard-coded algorthms with the benifits of programatic creation.
 
Remus Stratulat
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Jim - I'm sorry that I misslead you, I guess my example was not so fortunately choosen. You are right it is not the actual problem but in writing...
Rob - I can hard code this because I know what I'm searching for. At least this time. So, about that generative programming thing, can you point me in the right direction so I can give it a study?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it's a pretty big topic. Search google for "generative programming."
Here's one java-specific reference that came up on the first page- I haven't actually read this in detail but if you skim the summary and overview, it describes generative programming at a high level.
http://www.voelter.de/data/pub/jeneratorPaper.pdf
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't try it, but the following might be even faster:

BTW, are you really *sure* you need this very fast? That is, did you profile your program and found this to be a bottleneck?
[ April 14, 2002: Message edited by: Ilja Preuss ]
 
Remus Stratulat
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes, it's somehow a bottleneck. And your solution helps.
So until now we've got 3 versions and in a not very well conducted test I got this results:
ch1 last: 30
ch2 last: 119
ch3 last: 6
for 1000000 iterations, where ch1 method for 'case', ch2 method for 'indexOf' and ch3 method for 'boolean[]'. Time is in milliseconds.
If somebody have a better solution ...
Remus Stratulat
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Remus Stratulat:
Well yes, it's somehow a bottleneck.
[...]
If somebody have a better solution ...

If you could provide more context, (that is, why do you need to do this and why do you think it is a bottleneck), it could really help to find a better solution.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic