1

I am trying to test how inline function would save time for C++ compiler. So I write a demo code to test the running time difference between function using inline (f1) and another function not using inline(f2). However, after test, I found that the inline case will cost much more than not inline case. And I tried to change the test order of f1 and f2 in main function(test f2 cost time firt), in which case, f2 will cost less . Therefore, seems that the result will alwayss be the one in the front clock will cost more, there is no connection with inline or not inline. Why inline function in this case cost more time? code as follows:

#include <stdio.h>
#include <time.h>
#include <iostream>
#include <sstream>
using namespace std;

inline const string& f1(const string &s1, const string &s2)
{
    return s1.size() < s2.size() ? s1 : s2;
}

const string& f2(const string &s1, const string &s2)
{
    return s1.size() < s2.size() ? s1 : s2;
}

int main ()
{
   clock_t start_t,end_t;
   char s[10]="1000";
   static int N = 100000;
   clock_t start2_t,end2_t;
   char s2[10]="1000";
   static int i =0;

   start_t = clock();
   for(i = 0;i<N;i++)
       f1("100",s);
   end_t = clock();

   start2_t = clock();
   for(i = 0 ;i<N;i++)
       f2("100",s);
   end2_t = clock();

   printf("Inline:It took %d .\n",(int)(end_t - start_t)); 
   printf("No-Inline:It took %d .\n",(int)(end2_t - start2_t)); 
}

Here comes result:

    Inline:It took 10246 .
    No-Inline:It took 9385 .
xiesiyuan
  • 21
  • 1
  • very strange ! I am using g++ in linux. – xiesiyuan Nov 18 '18 at 13:59
  • _"Therefore, seems that the result will alwayss be the one in the front clock will cost more, there is no connection with inline or not inline. Why inline function in this case cost more time? "_ you seem to have some kind of confusion. – Passer By Nov 18 '18 at 14:05
  • 2
    Did you turn on compiler optimizations? – user202729 Nov 18 '18 at 14:06
  • Increase your **N** then try. Compile in release mode – snake_style Nov 18 '18 at 14:07
  • i just using g++ command to compile it in Ubuntu. g++ default will do some compiler optimizations. Yes, I am confused, because the result is very strange. Everyone can try it on you own computer... the result will be the same. Inline function cost more time. – xiesiyuan Nov 18 '18 at 14:13
  • g++ test.cpp -o test – xiesiyuan Nov 18 '18 at 14:16
  • N increase , nothing change. The inline function will cost more time. – xiesiyuan Nov 18 '18 at 14:20
  • The `inline` keyword has nothing to do with whether a function gets inlined or not. It's purely related to ODR and linking. – Jesper Juhl Nov 18 '18 at 14:26
  • @xiesiyuan throw in `#pragma GCC inline` at the top of your code. This gave me up to ~800ms difference at `N = 10^8`, with inline coming ahead of non-inline. – TrebledJ Nov 18 '18 at 14:29
  • 1
    Just note that `inline` can be weird and the compiler will do its own thing. [_Read more..._](https://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method) – TrebledJ Nov 18 '18 at 14:31
  • Have you used optimization? Use `-O2` or `-O3` compiler flag. – geza Nov 18 '18 at 14:33
  • I tried from -O0 to -O3. All the case are the same. – xiesiyuan Nov 18 '18 at 15:17
  • https://stackoverflow.com/a/16156167/128511 – gman Nov 18 '18 at 17:06

1 Answers1

2

Using naive performance metrics like this is often going to give confusing results. Optimization settings being one of the biggest confusions, release build (optimized):

/mnt/v/CLionProjects/StackOverflow/cmake-build-release/StackOverflow
Inline:It took 0 .
No-Inline:It took 0 .

Process finished with exit code 0

Debug build (non-optimized):

/mnt/v/CLionProjects/StackOverflow/cmake-build-debug/StackOverflow
Inline:It took 15625 .
No-Inline:It took 15625 .

Process finished with exit code 0

The optimized runs take no time because the compiler recognizes that the code does not accomplish anything (except spend CPU cycles). Performance metrics are difficult to design to ensure they are actually testing what you think they are testing, especially with optimizations turned on. But what is most often wanted is the performance of the optimized code. Unless you actually put the effort into designing good performance tests the best thing is often to write your application code and test with real world data, or believe what experts tell you. While I'm not a compiler and object code generation expert, what I have learned over the years is that advising the compiler to inline code is asking it to favor speed over size, and asking for the compiler to not inline code is asking it to favor size over speed. But neither request is binding on the compiler.

Richard
  • 8,920
  • 2
  • 18
  • 24