inline functions

C

chinu

hi all,
i did a small experiment to check the effect of inline as a function.
program is like this.

inline int fun();
int main(){

unsigned int start=0,end=0;
asm("rdtsc\t\n""mov %%eax, %0":"=r"(start));
fun();
asm("rdtsc\t\n""mov %%eax, %0":"=r"(end));

printf("No of ticks taken for execution is %d",end-start);

}

inline int fun(){

int x=0;
for(;x<100;x++);
}

i ran same program after removing inline also.. both time, execution
clicks are same, not shwoing any advantages of inline..Does compiler
automatically treat a function inline if function is small..
thanks in advance.

chinmay
 
M

Mark Odell

chinu said:
hi all,
i did a small experiment to check the effect of inline as a function.
program is like this.

inline int fun();
int main(){

unsigned int start=0,end=0;
asm("rdtsc\t\n""mov %%eax, %0":"=r"(start));
fun();
asm("rdtsc\t\n""mov %%eax, %0":"=r"(end));

printf("No of ticks taken for execution is %d",end-start);

}

inline int fun(){

int x=0;
for(;x<100;x++);
}

i ran same program after removing inline also.. both time, execution
clicks are same, not shwoing any advantages of inline..Does compiler
automatically treat a function inline if function is small..
thanks in advance.

It certainly may. However, I'm pretty sure 'inline' is not part of ISO
C so it's up to your compiler, not the language to determine how
inlining works, if at all.
 
V

Vladimir Oka

chinu said:
hi all,
i did a small experiment to check the effect of inline as a function.
program is like this.

inline int fun();
int main(){

unsigned int start=0,end=0;
asm("rdtsc\t\n""mov %%eax, %0":"=r"(start));
fun();
asm("rdtsc\t\n""mov %%eax, %0":"=r"(end));

printf("No of ticks taken for execution is %d",end-start);

}

inline int fun(){

int x=0;
for(;x<100;x++);
}

i ran same program after removing inline also.. both time, execution
clicks are same, not shwoing any advantages of inline..Does compiler
automatically treat a function inline if function is small..
thanks in advance.

The `inline` (much like `register`) is just a /suggestion/ to the
compiler to make the function call as fast as possible. What happens in
reality is implementation dependant. Here's a quote from the Standard:

6.7.4p5
A function declared with an inline function specifier is an inline
function.
The function specifier may appear more than once; the behavior is the
same as if it appeared only once. Making a function an inline
function
suggests that calls to the function be as fast as possible. The
extent
to which such suggestions are effective is implementation-defined.

Your particular implementation may document what exactly happens when
you declare an `inline` function`.

PS
Peppering your code with non-standard C constructs is frowned upon
here.
 
V

Vladimir Oka

Mark said:
However, I'm pretty sure 'inline' is not part of ISO C so it's up to your compiler,
not the language to determine how inlining works, if at all.

It is in C99, and as you say, it's implementation defined what really
happens.
 
B

bert

chinu said:
hi all,
i did a small experiment to check the effect of inline as a function.
program is like this.

inline int fun();
int main(){

unsigned int start=0,end=0;
asm("rdtsc\t\n""mov %%eax, %0":"=r"(start));
fun();
asm("rdtsc\t\n""mov %%eax, %0":"=r"(end));

printf("No of ticks taken for execution is %d",end-start);

}

inline int fun(){

int x=0;
for(;x<100;x++);
}

i ran same program after removing inline also.. both time, execution
clicks are same, not shwoing any advantages of inline..Does compiler
automatically treat a function inline if function is small..
thanks in advance.

chinmay

A sufficiently clever compiler might have optimised
your fun( ) function into a null function, or your call
of it into a null statement.
--
 
T

Tim Prince

chinu said:
hi all,
i did a small experiment to check the effect of inline as a function.
program is like this.

inline int fun();
int main(){

unsigned int start=0,end=0;
asm("rdtsc\t\n""mov %%eax, %0":"=r"(start));
fun();
asm("rdtsc\t\n""mov %%eax, %0":"=r"(end));

printf("No of ticks taken for execution is %d",end-start);

}

inline int fun(){

int x=0;
for(;x<100;x++);
}

i ran same program after removing inline also.. both time, execution
clicks are same, not shwoing any advantages of inline..Does compiler
automatically treat a function inline if function is small..
thanks in advance.

chinmay
If a compiler implements inlining optimization, it most likely is able
to remove the dead code you wrote. This example surely doesn't do what
you appear to think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top