| |

VerySource

 Forgot password?
 Register
Search
View: 689|Reply: 4

A test inline example question, I hope you all help

[Copy link]

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-25 17:00:01
| Show all posts |Read mode
Qian Neng's C ++ has an example of such an inline function:
#include <iostream>
#include <time.h>
#include <conio.h>
using namespace std;
int calc1 (int x, int y) {return x + y;}
inline int calc2 (int x, int y) {return x + y;}
main ()
{int x [1000], y [1000], z [1000];
 clock_t t = clock ();
 for (int i = 0; i <1000; i ++)
  for (int j = 0; j <1000; j ++)
   for (int k = 0; k <1000; k ++)
     z [i] = calc1 (x [j], y [k]);
 cout << "NOT USING INLINE:" << (clock ()-t) / CLK_TCK << "seconds\n";
   t = clock ();
    for (int i = 0; i <1000; i ++)
     for (int j = 0; j <1000; j ++)
      for (int k = 0; k <1000; k ++)
       z [i] = calc2 (x [j], y [k]);
 cout << "USING INLINE:" << (clock ()-t) / CLK_TCK << "seconds\n";
 getchar ();
 }

The result in the book is that the function without inlining is 8.281 seconds
The function with inlining is 2.437 seconds.
The two functions obtained by my machine are all 10 seconds, which fainted me.
Can anyone help me out ... Thanks in advance.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-15 09:30:01
| Show all posts
Compilers that don't, different compilation options, the results will be different, what's wrong with this
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-15 23:00:01
| Show all posts
If optimization is added, it is possible that your function will be automatically inlined.

So you can try to use the compiled command without optimization, and don't add debugging information,
Re-run may have different results

-IMHO, it is sometimes not easy to exclude all compiler optimizations, but is inlining really true?
In many compilers you can control yourself, unless your inlining is really unreasonable

(An example is if your inlining is a recursive function, then when the recursion reaches a certain level,
Or that the number of recursion layers exceeds the limit, then the compiler still uses a non-inline method to avoid the uncontrolled expansion of binary code)
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-16 00:30:02
| Show all posts
Some of the off-topics, your problem may be a sentence: although you write a non-inline function, but
The compiler felt that your non-inline function was simple enough, so it was treated as inline

So all you have to do is tell the compiler, don't just inline, just as I said! !!
For example vc compiler, you can use this command line to compile
cl / EHsc / Ob1 a.cpp
Reply

Use magic Report

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Japan

 Author| Post time: 2020-4-16 19:00:01
| Show all posts
I use dev C ++, no VC
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list