By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Function vs inline vs direct read speeds

P: 93
Well, the results arouse curiosity. Check out for yourselves.

[LINK]
http://www.thescripts.com/forum/threadnav617160-2-10.html
[/LINK]


Hi, I discovered this just now.

Code:

#include <windows.h> #include <mmsystem.h> #include <iostream.h> class TestClass { public: int variable; int ReturnVariable(); //Only the declaration. int ReturnVariableInline() {return variable;}; //inline. TestClass(); ~TestClass(); }; int TestClass::ReturnVariable() { return variable; } TestClass::TestClass() { variable = 0; } TestClass::~TestClass() {} int main() { TestClass test; long begin, end; timeBeginPeriod(1); int a = 0; begin = timeGetTime(); for(int i = 0; i < 10000000 ; i++) { a += test.ReturnVariable(); } end = timeGetTime(); cout << "Returning by a function : time :" << end - begin << endl; begin = timeGetTime(); for(i = 0; i < 10000000 ; i++) { a += test.ReturnVariableInline(); } end = timeGetTime(); cout << "Returning by an inline function : time :" << end - begin << endl; begin = timeGetTime(); for(i = 0; i < 10000000 ; i++) { a += test.variable; } end = timeGetTime(); cout << "Simply reading the value : time :" << end - begin << endl; return 0; }




the output on my machine is like this :

373
338
23

So, an inline function is faster than an ordinary one. But just reading the value is way faster. Are there any mistakes here? Because if there aren't any, I am never encapsulating data when I am striving to get an efficient program.
Mar 17 '07 #1
Share this Question
Share on Google+
1 Reply


P: 93
[quote=DumRat]Well, the results arouse curiosity. Check out for yourselves.

http://www.thescripts.com/forum/thre...7160-2-10.html

Sorry for the messy code. Check the link.
Mar 17 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.