-2

I have worked on C++ earlier but don't have that much knowledge on basics. So started learning it from basics (I found comparing code with assembly and learning is a fun and I find I could understand the actual concept better)

I was suggested by few to learn a optimized version of assembly rather than non-optimized version? But I'm literally confused with the optimized version assembly? which is preferred for learning basics (while comparing with assembly).

I'm using Visual Studio Community 2015

Please help!!!

infinite loop
  • 1,309
  • 9
  • 19
  • Like performance measurements, you new to look at how the code will run in production to really know what is going on. – NathanOliver Jun 05 '17 at 14:53
  • @NathanOliver So, you suggest going with a release version? – infinite loop Jun 05 '17 at 14:54
  • 1
    I'm guessing you mean assembly, not machine code. Do note that optimized builds may produce code which looks nothing like the program you wrote, yet it has the same output. Non-optimized will be easier to understand as a beginner. – DeiDei Jun 05 '17 at 14:54
  • 2
    Looks like you are heading for a giant waste of time. Unoptimized assembly does not reflect how stuff actually works in the end and optimized assembly is hard to read for any non-trivial example. Better read a good C++ book, more learning per time. – Baum mit Augen Jun 05 '17 at 14:56
  • 2
    @infiniteloop Yes. The un-optimized version of two codes may look the same but once optimization is turned on you can get wildly different code. – NathanOliver Jun 05 '17 at 14:56
  • @DeiDei, Yes!!! Edited. So its better to go with debug assembly? – infinite loop Jun 05 '17 at 14:57
  • 1
    Generally you are studying foundations of language not quite the basics. I wouldn't say this is good way to learn, but if you find it entertaining, probably only real value you can get is either studying release/production which is non-optimized. This should be pretty straightforward to understand. After that you might compare unoptimized code with optimized one to get a grasp of what compiler tricks are being used. But this is more useful for compiler writing than writing C++ (unless you do optimizations in veeeery specific scenarios) – Tomasz Plaskota Jun 05 '17 at 14:57
  • 2
    @infiniteloop No, reading the debug assembly is useless and misleading. You will not learn how C++ programs work in the real world from that because real-world binaries are always optimized. – Baum mit Augen Jun 05 '17 at 14:58
  • Looking at assembly isn't going to teach you a thing about C++. C++ is a language specification that you need to learn by reading [books](https://stackoverflow.com/q/388242/1889329). – IInspectable Jun 05 '17 at 15:16
  • I would like to suggest you to explore this web site: https://godbolt.org/ – tty6 Jun 05 '17 at 15:19

1 Answers1

2

It depends on what you want to see. Unoptimized code will more closely follow your code and will probably be more understandable. While when you enable optimizations, you might see what the compiler is really doing with your code. For example in the code below, the compiler will optimize everything out and leave just the return value computed at compile time.

int myFunc() {
    int x = 30;
    x = x + 12;
    return x;
}

So my answer is to use both.

Sam Hartman
  • 6,210
  • 3
  • 23
  • 40
Marek Vitek
  • 1,573
  • 9
  • 20