Why can't C++ be optimized by the compiler when it is compiled?

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 5218 Views - Last Post: 28 April 2012 - 01:34 AM

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Why can't C++ be optimized by the compiler when it is compiled?

Posted 09 November 2011 - 11:43 AM

I recently started loving Java a lot and decided to compare it with C++. Java was self-optimized but C++ is not - you can write terrible C++ code and the compiler will directly translate it to an exe but Java optimizes your code extremely well while it gets run and this makes up for it having to interpret rather than compile but imagine an optimized C++ . . . :) Or is that a horrible/near-impossible idea? Feel free to crush my dreams, lol.

Is This A Good Question/Topic? 0
  • +

Replies To: Why can't C++ be optimized by the compiler when it is compiled?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 09 November 2011 - 12:34 PM

When you compile your C/C++ code you can have the compiler optimize this code. Using gcc you have several different optimization levels. You can also optimize for size or speed. See this link: gcc Optimize options

Jim
Was This Post Helpful? 2
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5758
  • View blog
  • Posts: 12,573
  • Joined: 16-October 07

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 09 November 2011 - 01:39 PM

Every compiler optimizes. That, it guesses at the best way to do what it thinks you're asking. In C++ it generally assumes you really meant to do what you're asking. The same in Java, to an extent. It's just that Java offers you less avenues of self abuse. A C++ programmer would call that less flexible. It's a perspective and neither is really wrong.

Computers write crappy code. Always have, always will. Some optimizing is computers writing code, rather than just taking best advantage of the situation. That's why optimization levels and flags exist; in C++.

If you have Java GUI code written in a designer, any good Java programmer will tell you they can hand code better. If you have C++ code, compiled into assembly, any assembly programmer will tell you they could hand code better. It really depends on where you want to spend your time.
Was This Post Helpful? 3
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 09 November 2011 - 01:47 PM

what gave you the notion that C++ can't be optimized? C++ can and is optimized extensively, im not sure how much Java optimizes but it seems like it wouldn't really optimize more than C++(i could be very wrong here though).

This post has been edited by ishkabible: 09 November 2011 - 01:52 PM

Was This Post Helpful? 1
  • +
  • -

#5 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 10 November 2011 - 08:18 PM

The Java book I was reading said that people usually supposed that Java would have a slower performance but because of the high optimization, it nearly matches in performance with C, C++, C# etc.

Therefore I thought that C++ was not optimized since the author implied that. I guess he just wanted his book to sell, lol. :)
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 11 November 2011 - 06:15 AM

> If you have C++ code, compiled into assembly, any assembly programmer will tell you they could hand code better.

No, they won't. In fact, the usual discussion is "Look at the crazy optimization the compiler generated.". Optimization is an algorithmic game, except in cases where it's not possible to express the semantics efficiently. These include bit level tricks (e.g. use the carry flag) or C makes it very difficult to express the behavior outright (jump and predictive behavior in interpreters) or targeting specific instruction sets (like AES-NI) which forces the author to resort to assembly.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5758
  • View blog
  • Posts: 12,573
  • Joined: 16-October 07

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 11 November 2011 - 07:00 AM

View PostOler1s, on 11 November 2011 - 09:15 AM, said:

No, they won't.


You're talking to the wrong programmers. ;)

An assembly programmer is looking at the bark of they tree. A decent one will see some trick specific to the case they're looking at that would improve it. A case so specific that the compiler wouldn't have it in it's lexicon.

Programmers know what it supposed to happen and why. Compilers merely know to do what you told them and how to best do that in the most general context. A programmer can take advantage of patterns a the most sophisticated software can't see. This is, fundamentally, why we still have a job.

Can there be some lot level compiler optimization that I couldn't even conceive of that will do a better job in a certain context? Of course. But that's not what I was talking about.
Was This Post Helpful? 4
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 11 November 2011 - 07:57 AM

i think i can, with effort, produce better code than GCC actually. various extremely specific optimizations can be made that, as baavgai said, "out of the compiler's lexicon". it's unlikely that i will produce all the bit level hacks using bit-wise instructions(gcc seems to use them A LOT) but i can , on a more general scale, optimize control flow and expressions better than a compiler.
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 12 November 2011 - 11:08 AM

Quote

A decent one will see some trick specific to the case they're looking at that would improve it. A case so specific that the compiler wouldn't have it in it's lexicon.
Like what?

What tricks are you talking about? Give examples. The hardcore assembly writers are dumping intermediate assembly and analyzing it.

I see this statement all the time about these particular cases that an assembly programmer sees, repeated over and over, without substantiation. Give an example.

Quote

A programmer can take advantage of patterns a the most sophisticated software can't see.
No. The amount of information is severely complex. This isn't 1980. You're balancing the huge x86 instruction set with concerns like i-cache and d-cache usage, instruction pipelining, instruction size, register usage, and so on.

Your compiler is generally better at this.

I have an exercise for all of you. Write good C code (and use intrinsics when appropriate). Dump assembly. Explain the optimizations done by the compiler.

Because if you can't understand the faults in the compiler's code, you aren't going to do any better.

This post has been edited by Oler1s: 12 November 2011 - 11:08 AM

Was This Post Helpful? 1
  • +
  • -

#10 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 12 November 2011 - 06:25 PM

Most Assembly programmers (not newbies) optimize many times without knowing it. It comes from knowing each processor they are targeting and having the Intel and AMD manuals. Compilers use algorithms, Asm programmers use their brain and experience. When to use add eax, 1 instead of inc eax , when to use cmp, test, or, to check a value. When and how to unroll a loop... there are plenty "tricks of the trade". Compilers nowadays do a good job at optimizations, but a seasoned Assembly programmer can most likely do better as they can see the task at hand and know the desired outcome. A compiler just guesses.
Was This Post Helpful? 2
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 12 November 2011 - 06:35 PM

> Most Assembly programmers (not newbies) optimize many times without knowing it

False. By the time you are doing an optimization, you have already identified hotspots in your program, which means you profiled, you have dumped assembly, inspected, analyzed for the flaws, and then figured out what to do to write something more efficient. It's like saying experienced programmers write code without knowing it. No, that's ridiculous. You are doing some very explicit things.

> Compilers use algorithms

Yes. Algorithms, to solve complex problems. Humans are not good at doing those sort of calculations.

> When to use add eax, 1 instead of inc eax , when to use cmp, test, or, to check a value.

Which is harder than you say. Do any of you here saying this stuff about assembly programmers, actually do any assembly level optimization yourself??

> A compiler just guesses

A compiler is not guessing. It's algorithmically solving. What do you think the "optimization" portion of a compiler is??
Was This Post Helpful? 0
  • +
  • -

#12 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 12 November 2011 - 06:39 PM

Well, being that I write all my apps in Assembly then yes I do optimizations when and where needed.

>>you have dumped assembly, inspected, analyzed for the flaws

No, my source is in Assembly :)

This is one of those topics that can go back and forth like a topic in Religion..
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5758
  • View blog
  • Posts: 12,573
  • Joined: 16-October 07

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 12 November 2011 - 07:17 PM

View PostOler1s, on 12 November 2011 - 02:08 PM, said:

I see this statement all the time about these particular cases that an assembly programmer sees, repeated over and over, without substantiation. Give an example.


So, I fired up an editor and did a "gcc -S ..." and said screw it. We tell novices all the time not to use globals, magic numbers, the importance of abstraction, code reuse, modularization, etc. In most cases, there is no final word "example" we can give them justify these assertions. Only the wisdom of hard earned personal experience and perhaps the prevailing believe of the software development industry.

You seem to have some inexplicable emotional investment in the idea that my claims are false. So be it. I respectfully disagree.

Some links to further support my claim:

The Great Debate: "Will Compilers Ever Produce Code as Good as an Expert Assembly Language Programmer?" This one is spot on topic and well done.

Programming Optimization. General stuff, with a focus on C, some interesting observations. And perhaps insight into why a C programmer might turn to assembly.

GCC makes a mess. I might have offered something like this as my "example," but this guy already did all the work.

Microsoft Visual C++ Floating-Point Optimization. No assembly here at all, but rather the manual tricks a C++ programmer can use to get the most out of their compiler.

As a final appeal to reason, I can only offer that entire industries pay people to optimize perfectly good C code at the assembly level. Believing that the best an optimizing compiler can do can still be bested by an experienced programmer.
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 13 November 2011 - 01:48 PM

@hulla -- Java has a longer way to go. It is a much higher level language than C/C++ and so there is much more to optimize.

The big optimization in an language like Java and C# is something called a JIT (Just In Time - compiler) which compiles some kind of "byte code" into native instructions at run time while other instructions may be "interpreted".

So in Java the compiler produces byte code - at it can optimize while it does using many of the same techniques that C and C++ compilers do. HOWEVER the code produced is some kind of intermediate byte-code which still needs to be interpreted by the Java-virtual machine. However most JVM's have a JIT which can farther compile the byte-code down to native code.

So if I write an algorithm for say multiplying matrices the Java and C programs can essentially be executing exactly (at least in theory here) the same machine instructions for that function! Pretty neat.
Was This Post Helpful? 1
  • +
  • -

#15 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Why can't C++ be optimized by the compiler when it is compiled?

Posted 14 November 2011 - 02:23 AM

Quote

So if I write an algorithm for say multiplying matrices the Java and C programs can essentially be executing exactly (at least in theory here) the same machine instructions for that function! Pretty neat.


So because the JIT compiled it, the execution performance is the same?
Awesome!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2