Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » ({A stupid question.})

Credits go to Kris Asick, spellcaster, and Tobias Dammers for helping out!
This thread is locked; no one can reply to it. rss feed Print
({A stupid question.})
type568
Member #8,381
March 2007
avatar

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
Member #1,424
July 2001

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

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

type568
Member #8,381
March 2007
avatar

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
Member #1,493
September 2001
avatar

Nope.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Tobias Dammers
Member #2,604
August 2002
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

type568
Member #8,381
March 2007
avatar

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

Go to: