3

I was wondering if any of these samples works "faster" than the other. I know there can't be a big difference, but I just want to know if there's any difference.

CODE1:

a+b=c;
c=c*c;
d=c*a;

CODE2:

a+b=c,c=c*c,d=c*a;

So does it matter if I use , or ;?

Just asking... :D

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
XCS
  • 27,244
  • 26
  • 101
  • 151
  • 3
    I suspect not. (I assume the first line is backwards.) You know you can just look at the assembly code. The only real difference is that the second case is an expression, so it yields the value `d`. – Mike Dunlavey Feb 04 '11 at 20:28
  • 1
    in such cases, it helps to compare the assembly produced by the compiler. of course, generate it using your release build settings if speed/instructions is what you want to compare. – justin Feb 04 '11 at 20:30
  • 1
    Why do you ask "which one is faster?" rather than asking "which one is more readable?" – David Heffernan Feb 04 '11 at 20:31
  • I am not asking which one is faster, I only know if there's any speed difference. I was just curious xD – XCS Feb 04 '11 at 20:35
  • http://stackoverflow.com/questions/2087026/effect-of-using-a-comma-instead-of-a-semi-colon-in-c-and-c – Ciro Santilli OurBigBook.com Jul 02 '15 at 07:09

4 Answers4

4

The number of lines of a program is not indicative of its speed. To answer your question: no, there is no difference in speed between the two forms you posted. If you look at the assembly code generated by the compiler for each program, you will see it's exactly the same.

How to read the assembly output of a C program

Community
  • 1
  • 1
Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
2

No difference in terms of speed.

Jimmy
  • 637
  • 5
  • 13
  • 1
    Also, it's a bit of a repost. http://stackoverflow.com/questions/2087026/effect-of-using-a-comma-instead-of-a-semi-colon-in-c-and-c – Jimmy Feb 04 '11 at 20:29
2

There should not be any difference. However, it depends completely on the compiler. There is no way to know for certain whether your compiler/interpreter generated different assembly based on different code that you entered until you look at the assembly generated.

In Visual Studio you can view the assembly like so: http://msdn.microsoft.com/en-us/library/a3cwf295.aspx

In general, remember that the code you write in C++ is scanned by a program which decides best how to generate assembly for you. So in most cases, syntatic sugar like that will generate identical assembly code to the longer version.

More importantly, you should stop worrying about the difference in speed here. If speed is a concern, always look to your algorithm first, long before tiny differences like these.

Olhovsky
  • 5,466
  • 3
  • 36
  • 47
  • Thanks for you're answear. No, I was not worrying about the speed of a program, just... this question struck me :)) . Now I can sleep well... :D – XCS Feb 04 '11 at 20:37
0

In general the comma operator is not needed at all and often enough it is only used to write confusing code for the sake of fishy goals. For example more than once that I saw code like this

if (expression)
  statement1,
  statement2,
  statement3;

Just for the 'goal' to save the one or two extra lines for { and }.

My advice:
a) simply forget about the existence of the comma operator!
b) don't even think about micro optimization like this, instead look for something real to optimize maybe a loop, or the number of c-tors being called or eliminating calls to implicit conversion operators. A single one of such an optimization will do your program real good.

Thomas
  • 1