({A stupid question.})
type568

I have a question, that I had been thinking about for long time..

Is there ANY difference between

if(something)
    {
    variable+=(25*17);
    }

and

if(something)
    variable+=25*17;

If there is, what exactly, if no.. Thanks.

Kris Asick

Nope.

However, there would be a substantial difference between:

if (something)
{
  variable1 += 25 * 17;
  variable2 -= 15 * 28;
}

and

if (something)
  variable1 += 25 * 17;
  variable2 -= 15 * 28;

All the { } brackets do is group blocks of code together. To the compiler, a section inside { } brackets is sort of like a single command which does everything inside it.

The compiler doesn't actually check indents, new-lines, or anything like that. (Excepting # commands like #include and #define which are normally terminated by new lines as they are compiler directives, not code.)

For instance, if I wanted to set a sprite to random pixels, I might do something like this in my code:

for (z = 0; z < sprite->w; z++) for (zz = 0; zz < sprite->h; zz++)
  putpixel(sprite,z,zz,rand_val(0,255));

Which is effectively the same thing as:

for (z = 0; z < sprite->w; z++)
{
  for (zz = 0; zz < sprite->h; zz++)
  {
    putpixel(sprite,z,zz,rand_val(0,255));
  }
}

Those semicolons aren't just for show. They tell the compiler to terminate processing of a specific command or assignment. You can write an entire program without ever using indents or carriage returns. Of course, it would be almost impossible to read, but it would work!

--- Kris Asick (Gemini)
--- http://www.pixelships.com

type568

Thanks, i know that though.. except for the #..

But, I mean ANY difference in the resulting BIN/EXE/DLL file, and the performance..

if(something)
    {{
    something=anything;
    }}

if(something)
    {
    something=anything;
    }

Does this somehow differ? (i.e. second performed 0.001NS faster?)

EDIT:
Also printf("%f",(((hi))));
and printf("%f",hi);

spellcaster

Nope.

Tobias Dammers
Quote:

Does this somehow differ? (i.e. second performed 0.001NS faster?)

The resulting binary should be exactly the same. If it's not, your compiler is probably ancient, and you shouldn't be using it.

Extra brackets will increase compile time though, but probably not by any measureable amount of time (unless you surround each statement by thousands of extra {} brackets).

Anyway, this is nothing that should concern you. Code readably; if it's slow, optimize algorithms; if it's still slow, find the 1% of your code that causes the major part of the slowdown, and optimize that.

type568

Thanks Tobias. Thanks all, question is answered to my satisfaction. ^^

Thread #591571. Printed from Allegro.cc