Reputation: 598
Suppose I have some code, and I want to optimize it to the maximum, disregarding readability, maintainability, etc.
For this purpose, is there anyway to figure out how much time every basic action takes? I suppose this may be CPU dependent, but I'm not sure.
I mean stuff like cycling through a for
, assignments a=24
, mathematical operations 2+25
, etc.
Upvotes: 3
Views: 441
Reputation: 364288
Context matters. There aren't fixed costs for various Java language constructs that you can just add up to get even an approximation of a useful run-time estimate. The answer you seem to be hoping for doesn't exist.
Even if you did manage to correctly design a microbenchmark to measure some if()s
vs. a switch
, for example, the results would be heavily dependent on the surrounding code, the details of what the cases were, and the predictability of the branch. Making microbenchmarks is hard. You often end up measuring something other than what you intend to, unless you look at the machine instructions that actually run in your loop. It's also easy for a good compiler to optimize away your loop if you don't use the results, but then it's hard to use the results in a way that doesn't create more overhead than what you're trying to measure.
A good JIT-compiler JVM should generate machine code that's not too much worse than what you'd hope for, so if you have a good sense of how C compiles to ASM, that could be useful for java.
If you want to learn what is fast vs. slow on modern x86 microarchitectures, have a look at Agner Fog's guides.
A good profiling tool can help you figure out whether your code is CPU-bound or bottlenecked on memory (cache misses: memory bandwidth or latency), or branch mispredicts. I haven't done this for Java, but the standard tools (like Linux's perf
) probably work as long as you use a long enough run to hide the overhead of the JVM startup.
Upvotes: 7
Reputation: 585
You could do something like:
long startTime = System.currentTimeMillis();
// ---- Your code ----
long endTime = System.currentTimeMillis()
System.out.println("The previous line took " + (endTime - startTime) + " ms");
Upvotes: 0