• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bytecode curiosity

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read somewhere that compilers sometimes optimize our codes to make them faster or more efficient. I'm so interested in this. If I learn how to read java bytecode, will I be able to know how the compiler optimize my code by reading and understanding the bytecode?
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you will not. Almost all interesting optimizations are applied when the bytecode is transformed to machine code, not when your source code is transformed to bytecode.
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Compilers can optimize code in many different ways. In most JVMs, in fact, the JVM has the option to convert the bytecode into native machine-language instructions. And can even customize the generated code based on how it's been executed (the ration of "then"s versus "elses" in an "if" statement, for example. Only the simplest of bytecode interpreters would give you any chance of deducing runtime efficiency from simply looking at the bytecode. On top of that, even if you look at actual machine code, modern-day CPUs do so much internal pipelining, look-ahead processing and other tricks that you can no longer simply add up nanoseconds per instruction the way we used to. The actual execution time is so dependent on the data of the moment that you can only get a rough guess. And that's before externalities such as interrupts.

When it comes to optimizing, you can generally gain far, far more efficiency by use of an appropriate algorithm than you can by fiddling around with low-level instructions. A classic example I like to mention is when I changed a simple bubble sort for a shell sort. Most people would have gone for a quick- or heapsort, since they're "more efficient", but the data in this case was almost completely in order to begin with, barring several important exceptions that took too long to bubble. But if you pass data that's mostly sorted to a heapsort or quicksort, that's their worst-case modes and they'll perform really slow.

It's worth looking at how bytecodes work, but I wouldn't make it an essential part of my daily programming.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google for the “conversation with Brian Goetz” where he writes about writing dumb code. That explains that simple code is easier to optimise than if you try to optimise it yourself. This might be the link you want.
 
Franklin Wormwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. Guess I'll stop learning bytecode.
 
Tim Holloway
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No harm learning it. We're just cautioning you that it's not very useful to use it for optimization/
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can be useful to learn bytecode especially if you understand assembler. But for other uses, not optimisation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic