| Author |
Which is faster?
|
Remus Stratulat
Greenhorn
Joined: Jan 29, 2002
Posts: 14
|
|
It's a critical operation and I need to know which is faster: or Thanks
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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 ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Remus Stratulat
Greenhorn
Joined: Jan 29, 2002
Posts: 14
|
|
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
Joined: Nov 07, 2000
Posts: 621
|
|
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
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Remus- it sure looks contiguous. Does this mean the actual problem is different from what you posted?
|
 |
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
|
|
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.
|
Rob
SCJP 1.4
|
 |
Remus Stratulat
Greenhorn
Joined: Jan 29, 2002
Posts: 14
|
|
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
Joined: Jan 07, 2002
Posts: 2205
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
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 ]
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Remus Stratulat
Greenhorn
Joined: Jan 29, 2002
Posts: 14
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
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.
|
 |
 |
|
|
subject: Which is faster?
|
|
|