general understanding

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Yeti4000
Posts: 6
Joined: April 2nd, 2013, 6:09 pm

general understanding

Post by Yeti4000 » April 6th, 2013, 9:40 pm

hi
im new to programming and i have a quite general question
its a matter of general understanding compiling i guess

i was writing two versions of Putpixel

PutPixel( int x, int y, D3DCOLOR c )
{
pSysBuffer[ x + WIDTH * y ] = c;
}

and

PutPixel( int x, int y, int r, int g, int b)
{
PutPixel( int x, int y, D3DCOLOR_XRGB(r,g,b) );
}

my first thought when i wrote it is was that this second version would be slower
(of course only very very slightly) than a version that
does not pass the variables on to another method.. like this implementation:

PutPixel( int x, int y, int r, int g, int b)
{
pSysBuffer[ x + WIDTH * y ] = D3DCOLOR_XRGB( r,g,b );
}

so my question is:

would one version be "slower" than the other or would they after compiling just be like the same?
.... eventually they do the exact same thing...
does calling funktions itself cost time in the running project or are both of my implementations
just different ways to tell the compilter to do the exact same thing???..

i belief its the latter.. but i dont KNOW

english is not my native language and i feel like i couldnt exactly express what i wanted to say... but i hope you'll get it anyways...

User avatar
joelyboy94
Posts: 67
Joined: October 11th, 2012, 7:34 pm

Re: general understanding

Post by joelyboy94 » April 7th, 2013, 12:00 am

I think, though I am not certain, that the second one is slower, purely because D3D uses the D3DCOLOR type for storing and displaying pixels, and for the second it has to convert the r g b vales to a D3DCOLOR value for each pixel. However if you want to find out just try drawing a load of pixels using each function individually and use the timer to see which one is faster at runtime.
To be completely honest, like you said, I don't think it will make a difference to the performance of the program for the kind of level Chili's framework is aimed at.

simplicity
Posts: 18
Joined: July 10th, 2012, 6:55 pm

Re: general understanding

Post by simplicity » April 7th, 2013, 12:00 am

Yeti4000 wrote:hi
im new to programming and i have a quite general question
its a matter of general understanding compiling i guess

i was writing two versions of Putpixel

PutPixel( int x, int y, D3DCOLOR c )
{
pSysBuffer[ x + WIDTH * y ] = c;
}

and

PutPixel( int x, int y, int r, int g, int b)
{
PutPixel( int x, int y, D3DCOLOR_XRGB(r,g,b) );
}

my first thought when i wrote it is was that this second version would be slower
(of course only very very slightly) than a version that
does not pass the variables on to another method.. like this implementation:

PutPixel( int x, int y, int r, int g, int b)
{
pSysBuffer[ x + WIDTH * y ] = D3DCOLOR_XRGB( r,g,b );
}

so my question is:

would one version be "slower" than the other or would they after compiling just be like the same?
.... eventually they do the exact same thing...
does calling funktions itself cost time in the running project or are both of my implementations
just different ways to tell the compilter to do the exact same thing???..

i belief its the latter.. but i dont KNOW

english is not my native language and i feel like i couldnt exactly express what i wanted to say... but i hope you'll get it anyways...
The second one is better for memory reasons. Also, it's slower as the computer would have to create a copy of the D3DCOLOR_XRGB instead of creating three ints.

Yeti4000
Posts: 6
Joined: April 2nd, 2013, 6:09 pm

Re: general understanding

Post by Yeti4000 » April 7th, 2013, 11:36 am

i just watched chilis I7 tutorial...
and he actually answeser my question there.. to a certain point at least...

and the answer is:
Inlining

as far as i understand it, it means, that in my example the compiler

would, when i call

PutPixel( int x, int y, int r, int g, int b)
{
PutPixel( int x, int y, D3DCOLOR_XRGB(r,g,b) );
}

just internally replace the
PutPixel( int x, int y, D3DCOLOR_XRGB(r,g,b) ); -call
wit the ACTUAL code of PutPixel( int x, int y, D3DCOLOR_XRGB(r,g,b) );

that means there would be no difference in calling the two implementations of my
PutPixel( int x, int y, int r, int g, int b)..

however... i cant find a clear statement (goole etc) to which degree the compiler does that..
to how complex a method can be to be still inlinde...

but i guess it will work in cases like mine..

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: general understanding

Post by viruskiller » April 11th, 2013, 7:53 pm

i'm not quite sure if inlining works on it's own by default, i've read something that u need to add "inline" before the function call,anyway i've tested before different ways to speed up the put pixel function and the only way to get drastic results was NOT TO USE THE FUNCTION AT ALL.
i was drawing very large squares and when used put pixel fps would drop to 10-15 fps for a square of
900x900,but when using the code inside put pixel right in the square draw function the fps went straight to 60 , so that for me tells those function calls have some execution time and at 900x900 that multiply's alot , exactly 810000 times.
it may be only my hardware, or my compiler settings but u should test with yours and compare the results.

User avatar
Xanderxavier
Posts: 9
Joined: July 27th, 2012, 5:32 pm

Re: general understanding

Post by Xanderxavier » April 12th, 2013, 4:03 pm

You can put inline on it, however it doesn't necessarily mean the compiler will inline it basically it will make up its own mind weather something can be made inline or not putting inline on is basically like a programmer asking the compiler to do so, doesnt mean it will, however, it will also inline functions you dont specific as inline if it thinks its suitable and ignore ones you told it to if it thinks its unsuitable.

as the wiki page on it says :
The programmer has little or no control over which functions are inlined and which are not.
and
Mainstream C++ compilers like Microsoft Visual C++ and GCC support an option that lets the compilers automatically inline any suitable function, even those not marked as inline functions.
and most pertinently:
In various versions of the C and C++ programming languages, an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.
See : http://en.wikipedia.org/wiki/Inline_function

Don't forget to first look at cplusplus.com and wiki whenever you have a question most of the answers are there but a search away.

gl

Xander

Post Reply