calculating difference in timing in dynamic and static binding

consider the code bellow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<ctime>
#include<boost/progress.hpp>
using namespace std;
class parent
{
    public:
    virtual void dynamic_display(){
        cout<<"It is implemented in Parent class\n";
    }
};
 
class child:public parent
{
    
    static int static_count;
    static int dynamic_count;
    public:
    void static_display(){
        int temp,i;
        for (i=0;i<100000;i++)
        temp=temp+i;
        cout<<"Static Function is called"<< static_count<<"times\n";
        static_count=static_count+1;
    }
    void dynamic_display(){
        int temp,i;
        for (i=0;i<100000;i++)
        temp=temp+i;
        cout<<"Dynamic function is called"<<dynamic_count<<"times\n";
        dynamic_count=dynamic_count+1;
    }
};
int child::static_count=1;
int child::dynamic_count=1;
 
int main(){
    clock_t t1;
    clock_t t2;
    child  chld;
    //first call to static method
    t1=clock();
    cout<< "Calculating....";
    chld.static_display();
    t2=clock()-t1;
    cout<<"The number of processor clicks is" <<t2<<"time is"<<((float)t2/CLOCKS_PER_SEC)<<"\n"; 
    //First call to dynamic method
    t1=clock();
    cout<< "Calculating....";
    chld.dynamic_display();
    t2=clock()-t1;
    cout<<"The number of processor clicks is" <<t2<<"time is"<<((float)t2/CLOCKS_PER_SEC)<<"\n"; 
    //second call to static method
    chld.static_display();
    //second call to dynamic method
    chld.dynamic_display();
    //third call to static method
    chld.static_display();
    //third call to dynamic method
    chld.dynamic_display();
    return 0;
}


I am getting the following as output


Calculating....Static Function is called1times
The number of processor clicks is0time is0
Calculating....Dynamic function is called1times
The number of processor clicks is0time is0
Static Function is called2times
Dynamic function is called2times
Static Function is called3times
Dynamic function is called3times



i am actually trying to calculate the time to execute a statically binding method and a dynamically binded one.consider only the first four lines in my output.Why am i not getting the actual result.where have i gone wrong please advice.............
Would there be any actual difference in this case anyway - because you are
using a a statically allocated object:

child chld;// actual type of the object is known at compile time.


So the compiler can make a direct call to child::dynamic_display and the
vritual call mechanism would not be used.
Increase the value on line 21/28. I used 10000000 to get a time different from 0
i tried incrementing the values on line 21/28.but i still get 0 as my result....
i changed the object allocation to dynamic
by

 
Child *chld = new Child();


but still code gives 0 as the answer,is there something wrong with the way i use the clock function..........
would the way we create object's i.e in stack or heap, affect the way in which the methods are binnded (statically or dynamically) guestgulkan..........
Topic archived. No new replies allowed.