Thursday, December 21, 2006

Inline Function in C++

... .However, another good reason to inline is that you can sometimes speed up your program by inlining the right function. Instead of calling the function every time it is invoked, the compiler will replace the function call with a copy of the function body.
[from the page given in the link. Also read Why not inline everything?]

And here is my project for speed comparison of inline defined function and class defined function in Visual C++:

First: Copy and paste below codes with the correct name into header files in your project.

//Filename:MyMath.h
#include

using namespace std;

class Add{
public:
int add_inline( int a,int b){
return a+b;
}
int add_class_def(int a1,int b1);
};

========================================================

// Filename:stdafx.h
// include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Classes Reference and related electronic
// documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft C++ Libraries products.
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// turns off ATL's hiding of some common and often safely ignored warning messages
#define _ATL_ALL_WARNINGS
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0403
#endif
// TODO: this disables support for registering COM objects
// exported by this project since the project contains no
// COM objects or typelib. If you wish to export COM objects
// from this project, add a typelib and remove this line

#define _ATL_NO_COM_SUPPORT
#include

Second: Below codes are the cpp files for your project.

//Filename:Main.cpp
#include "MyMath.h"
#include "stdafx.h"
#include

using namespace std;
void main()
{
DWORD startTime,endTime,difTime;

int a=1;
int
b=2;
int c;
Add testAdd;
// use inline function declared and defined in header file MyMath.h
startTime=GetTickCount();
for(int i=0;i<1e9;i++){>
c=testAdd.add_inline(a,b);
}
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<<<">
//use function defined in other cpp file
startTime=GetTickCount();
for(int i=0;i<1e9;i++){
c=testAdd.add_class_def(a,b);
}
endTime=GetTickCount();
difTime=endTime-startTime;
cout<< "it took "<<<">
}
========================================================

//File name:class_def.cpp
#include "MyMath.h"
int Add::add_class_def(int a1,int b1){
return a1+b1;
}

========================================================


// Filename: stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
// PerformanceCounter.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Classes Reference and related electronic
// documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft C++ Libraries products.
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file


Results:
*test1


*test2 (In another computer with different configuration)

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home