Re: [Dev-C++] differnece between system pause and cin.get()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2003-02-18 08:23:25
|
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: =D4=F1=DF=F4=E7, 18 =D6=E5=E2=F1=EF=F5=DC=F1=E9=EF=F2 2003 9:42:16 =
=F0=EC=0D
To: Ioannis Vranos=0D
Cc: Dev-C++ Mailing List=0D
Subject: Re: [Dev-C++] differnece between system pause and cin.get()=0D
=0D
For register calling convention, the this pointer may be sent for free=0D
from one member of the class to another member of the class, since the=0D
register will already be initialized with this. For stack calling=0D
convention, it HAS to take extra time, since it's an extra 32-bit put pus=
h=0D
on the stack. Also, weith register calling, the first call HAVE to take=0D
extra time, since the compiler must load the address of the object before=
=0D
making the call.=0D
=0D
=0D
=3D=3D> Ok, so in few words you are telling that the regular function cal=
ls have
an additional extra cost because of the stack calling convention, while
member functions have that time cost only at the first call when it fetch=
es
it from memory to register. So we agree.=0D
=0D
=0D
=0D
Member functions always have a regular address. Your description of call=0D
through an offset in an array is the special case of a call to a virtual=0D
method.=0D
=0D
=0D
=3D=3D> No, no, no. Try assigning a member function to a pointer for a re=
gular
function. Try to print the "address" of a member function. The standard d=
oes
not restrict how member functions are implemented, only that they cannot =
be
expected to be regular functions. Usually, member functions are called
through an offset of an array.=0D
=0D
=0D
However, the constructor for the same class may still call=0D
directly without going to the VMT (Virtual Method Table). A call through=0D
the VMT is slower, since the compiler first have to load the VMT table=0D
address as an offset from the this pointer, and then do the call from an=0D
offset into the table.=0D
=0D
If you don't believe me, just ask the compiler to emit assembler output=0D
and have a peek.=0D
=0D
=0D
=3D=3D> You are out of range for me. :) I do not know assembly almost at =
all and
i am telling only what i have read/learned. But since we know C++ we can =
do
the test:=0D
=0D
#include <iostream>=0D
#include <ctime>=0D
#include <limits>=0D
#include <cstdio>=0D
=0D
class something=0D
{=0D
unsigned long x;=0D
=0D
public:=0D
something(int y=3D0):x(y) {}=0D
=0D
void test();=0D
friend void testf(something &);=0D
};=0D
=0D
=0D
=0D
int main()=0D
{=0D
using namespace std;=0D
=0D
clock_t t1, t2, t3, t4, temp;=0D
=0D
unsigned long i;=0D
=0D
something blade;=0D
=0D
cout<<"Number of iterations for each kind: "<<numeric_limits<unsigned
long>::max()/20<<endl;=0D
=0D
=0D
for (t1=3Dtemp=3Dclock(); !(t1-temp); t1=3Dclock())=0D
;=0D
=0D
=0D
for(i=3D0; i<numeric_limits<unsigned long>::max()/20; i++)=0D
blade.test();=0D
=0D
t2=3Dclock();=0D
=0D
cout<<"Time consumed by "<<numeric_limits<unsigned long>::max()/20<<" ca=
lls
of void something::test(): ";=0D
cout<<static_cast<double>(t2-t1)/CLOCKS_PER_SEC<<endl;=0D
=0D
=0D
for (t3=3Dtemp=3Dclock(); !(t3-temp); t3=3Dclock())=0D
;=0D
=0D
for(i=3D0; i<numeric_limits<unsigned long>::max()/20; i++)=0D
testf(blade);=0D
=0D
t4=3Dclock();=0D
=0D
cout<<"Time consumed by "<<numeric_limits<unsigned long>::max()/20<<" ca=
lls
of void testf(something &): ";=0D
cout<<static_cast<double>(t4-t3)/CLOCKS_PER_SEC<<endl;=0D
=0D
}=0D
=0D
=0D
void testf(something &x) { x.x++; }=0D
=0D
void something::test() { x++; }=0D
=0D
=0D
=3D=3D> This code in my system produces:=0D
=0D
C:\c>temp=0D
Number of iterations for each kind: 214748364=0D
Time consumed by 214748364 calls of void something::test(): 0.45=0D
Time consumed by 214748364 calls of void testf(something &): 0.461=0D
=0D
=0D
The difference is very slight, negligible. However after repeative runs,
testf() consistently remains the slower:=0D
=0D
C:\c>temp=0D
Number of iterations for each kind: 214748364=0D
Time consumed by 214748364 calls of void something::test(): 0.45=0D
Time consumed by 214748364 calls of void testf(something &): 0.461=0D
=0D
C:\c>temp=0D
Number of iterations for each kind: 214748364=0D
Time consumed by 214748364 calls of void something::test(): 0.45=0D
Time consumed by 214748364 calls of void testf(something &): 0.481=0D
=0D
C:\c>temp=0D
Number of iterations for each kind: 214748364=0D
Time consumed by 214748364 calls of void something::test(): 0.45=0D
Time consumed by 214748364 calls of void testf(something &): 0.481=0D
=0D
C:\c>temp=0D
Number of iterations for each kind: 214748364=0D
Time consumed by 214748364 calls of void something::test(): 0.45=0D
Time consumed by 214748364 calls of void testf(something &): 0.481=0D
=0D
=0D
=0D
=0D
Bottom line: As I said on my original mail, member calls are slower than=0D
regular calls, but these extra nanoseconds might be ignored for just abou=
t=0D
all programs out there, since the majority of the runtime is in other=0D
parts of the program.=0D
=0D
=0D
=3D=3D> I disagree, but that's life.=0D
=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |