because the computer will predict the outcome based on previous outcomes.
you realize that branch prediction is basically at the assembly level right? who knows what in the world happens to a java loop when digested by the jvm. now maybe the jvm does branch prediction but i don't know that and depending on the processor pipeline to do it is silly.
The JVM profiles branches to figure out which path is taken the most. It then keeps that path close to the branch (locality of next instruction) and optimizes assuming that path will usually be taken (e.g. putting error-handling code far away because it probably won't need it).
This isn't really branch prediction as much as structuring native to be friendly to the common path.
Branch prediction is a functionality of the cpu that uses previous outcomes of assembly functions in order to predict the next outcome. This can save time with long-looping code but can also cost a lot of time when your code only does a few loops at a time before changing the outcome, or branching.
I know what branch prediction is. Did you read my other responses. What I'm arguing is that when things are run through the jvm there's no telling what they compile down to in native code, let alone whether the processor's branch prediction facilities will be able to optimize.
Maybe you shouldn't ask questions that you know the answer to, and then get angry at someone who tells you something you already know.
when things are run through the jvm there's no telling what they compile down to in native code
Branch prediction is a separate architecture inside of the CPU and doesn't rely on the JVM whatsoever, as long as your program is using some kind of conditional function (if-then-else etc), the branch predictor is being utilized.
Man you people have terrible reading comprehension. The person responded to my post that clearly demonstrated that I knew what branch prediction was. I assumed he was saying something more insightful than just "it's inside the processor" because I assumed he read my post and was digging deeper, instead of just saying something irrelevant. Then all you idiots decided to further clarify something I made clear in my first post. Jesus
Branch prediction is a separate architecture inside of the CPU and doesn't rely on the JVM whatsoever, as long as your program is using some kind of conditional function (if-then-else etc), the branch predictor is being utilized.
What? It's not magic. If it doesn't get compiled down to a branch instruction in the processor's instruction set the branch prediction won't magically happen.
Maybe if you used some more of your vocabulary instead of trying to flame people for responding, you could have written two full sentences that gave more of a clue to how much you knew about branch prediction. If you knew what branch prediction was, you would know that it is internal to the processor. I don't see why you would think that the jvm would have a branch predictor when the CPU already has one, all the jvm has to do is send it a branch inst and the b.p. is being utilized.
What? It's not magic. If it doesn't get compiled down to a branch instruction in the processor's instruction set the branch prediction won't magically happen.
Sorry that I left out the step where the jvm compiles the code into assembly language; all conditional functions use branch instructions. I guess I assumed you knew that already and didnt need that explained?
Dude, you sound like a bit of a dick. Calling people "all you idiots" does not encourage people to give meaningful answers to your questions.
And yes, there is a way to tell what the bytecode gets compiled to in native code. It's not rocket science.
In most cases (assuming e.g. the loop isn't optimised away by the jit compiler) a loop with a counter will end up containing a conditional branch CPU instruction, and so will take advantage of CPU branch prediction logic.
I got upset because I got haphazardly constructed replies to my initial question. My first response was not snide or rude. It's the following ones that were.
In most cases (assuming e.g. the loop isn't optimised away by the jit compiler) a loop with a counter will end up containing a conditional branch CPU instruction, and so will take advantage of CPU branch prediction logic.
You're the second person to say this but what I'm saying is that that's very difficult to predict. You don't even know when the JIT will compile it to native code!
It's really not difficult to predict. Someone correct me if I'm wrong (I have a pretty limited understanding of run-time compilers), but I don't think that it is possible to compile a conditional statement and NOT have it end up with a branch-if function at the assembly level.
-11
u/ice109 Jul 05 '15
you realize that branch prediction is basically at the assembly level right? who knows what in the world happens to a java loop when digested by the jvm. now maybe the jvm does branch prediction but i don't know that and depending on the processor pipeline to do it is silly.