Hi Guys,
I need to check your knowledge for a bit, because i'm coming up at a brick wall.
Been doing an assignment for university on 'benchmarking' where we ran two little programs and gathered the time it took for 1 iteration of a loop running a calculation (addition, subtraction, multiplication and division)
Anyway, the Delphi programmed benchmarker always provided quicker times compared to the C++ programmed one.
Our lecturer is absolutely useless and giving us no support for it at all. Do you know of any reason why Delphi/Pascal code is quicker to run than C++ code? I enclose the two segments of code for your perusal, but by my gathering they look pretty much the same, apart from given differences in writing.
C++
Delphi
If you could provide any insight at all i'd be greatful.
I need to check your knowledge for a bit, because i'm coming up at a brick wall.
Been doing an assignment for university on 'benchmarking' where we ran two little programs and gathered the time it took for 1 iteration of a loop running a calculation (addition, subtraction, multiplication and division)
Anyway, the Delphi programmed benchmarker always provided quicker times compared to the C++ programmed one.
Our lecturer is absolutely useless and giving us no support for it at all. Do you know of any reason why Delphi/Pascal code is quicker to run than C++ code? I enclose the two segments of code for your perusal, but by my gathering they look pretty much the same, apart from given differences in writing.
C++
Code:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "time06.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::BtnAddClick(TObject *Sender)
{
SYSTEMTIME myTime;
int n,m,loops,powerOf10;
int a,b,c;
double x,y,z;
int timeStart, timeEnd;
loops=1 ;
powerOf10 = StrToInt(EdtPowerOfTen->Text);
for(m=0;m<powerOf10;m++)loops=loops*10;
GetLocalTime(&myTime);
Edit1->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeStart = myTime.wSecond*1000+myTime.wMilliseconds;
if (RBtnInteger->Checked)
for(n=0; n<loops; n= n+1) a = b+c; // Integer operations
if (RBtnFloat->Checked)
for(n=0; n<loops; n= n+1) x = y+z ;
GetLocalTime(&myTime);
Edit2->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeEnd = myTime.wSecond*1000+myTime.wMilliseconds;
Edit3->Text=IntToStr(timeEnd-timeStart);
Edit4->Text=FloatToStrF((timeEnd-timeStart)*1e6/loops,ffExponent,3,2);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
SYSTEMTIME myTime;
int n,m,loops,powerOf10;
int a,b,c;
double x,y,z;
int timeStart, timeEnd;
loops=1 ;
powerOf10 = StrToInt(EdtPowerOfTen->Text);
for(m=0;m<powerOf10;m++)loops=loops*10;
GetLocalTime(&myTime);
Edit1->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeStart = myTime.wSecond*1000+myTime.wMilliseconds;
for(n=0; n<loops; n= n+1){
// a = b /c; // Integer operations
// a = b*c;
x = y/z; // Real operations
// x = y*z;
// x = cos(.5);
// x = log(100);
// Form2->Caption = StrToInt(n);
// Memo1->Lines->SaveToFile("temp.tmp");
}
GetLocalTime(&myTime);
Edit2->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeEnd = myTime.wSecond*1000+myTime.wMilliseconds;
Edit3->Text=IntToStr(timeEnd-timeStart);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::BtnSubtractClick(TObject *Sender)
{
SYSTEMTIME myTime;
int n,m,loops,powerOf10;
int a,b,c;
double x,y,z;
int timeStart, timeEnd;
loops=1 ;
powerOf10 = StrToInt(EdtPowerOfTen->Text);
for(m=0;m<powerOf10;m++)loops=loops*10;
GetLocalTime(&myTime);
Edit1->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeStart = myTime.wSecond*1000+myTime.wMilliseconds;
if (RBtnInteger->Checked)
for(n=0; n<loops; n= n+1) a = b-c; // Integer operation
if (RBtnFloat->Checked)
for(n=0; n<loops; n= n+1) x = y-z ; //Float opertion
GetLocalTime(&myTime);
Edit2->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeEnd = myTime.wSecond*1000+myTime.wMilliseconds;
Edit3->Text=IntToStr(timeEnd-timeStart);
Edit4->Text=FloatToStrF((timeEnd-timeStart)*1e6/loops,ffExponent,3,2);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::BtnMultiplyClick(TObject *Sender)
{
SYSTEMTIME myTime;
int n,m,loops,powerOf10;
int a,b,c;
double x,y,z;
int timeStart, timeEnd;
loops=1 ;
powerOf10 = StrToInt(EdtPowerOfTen->Text);
for(m=0;m<powerOf10;m++)loops=loops*10;
GetLocalTime(&myTime);
Edit1->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeStart = myTime.wSecond*1000+myTime.wMilliseconds;
if (RBtnInteger->Checked)
for(n=0; n<loops; n= n+1) a = b*c; // Integer operations
if (RBtnFloat->Checked)
for(n=0; n<loops; n= n+1) x = y*z ;
GetLocalTime(&myTime);
Edit2->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeEnd = myTime.wSecond*1000+myTime.wMilliseconds;
Edit3->Text=IntToStr(timeEnd-timeStart);
Edit4->Text=FloatToStrF((timeEnd-timeStart)*1e6/loops,ffExponent,3,2);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::BtnDivideClick(TObject *Sender)
{
SYSTEMTIME myTime;
int n,m,loops,powerOf10;
int a,b,c;
double x,y,z;
int timeStart, timeEnd;
loops=1 ;
powerOf10 = StrToInt(EdtPowerOfTen->Text);
for(m=0;m<powerOf10;m++)loops=loops*10;
GetLocalTime(&myTime);
Edit1->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeStart = myTime.wSecond*1000+myTime.wMilliseconds;
if (RBtnInteger->Checked)
for(n=0; n<loops; n= n+1) a = b/c; // Integer operations
if (RBtnFloat->Checked)
for(n=0; n<loops; n= n+1) x = y/z ;
GetLocalTime(&myTime);
Edit2->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeEnd = myTime.wSecond*1000+myTime.wMilliseconds;
Edit3->Text=IntToStr(timeEnd-timeStart);
Edit4->Text=FloatToStrF((timeEnd-timeStart)*1e6/loops,ffExponent,3,2);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::BtnEmptyClick(TObject *Sender)
{
SYSTEMTIME myTime;
int n,m,loops,powerOf10;
int a,b,c;
double x,y,z;
int timeStart, timeEnd;
loops=1 ;
powerOf10 = StrToInt(EdtPowerOfTen->Text);
for(m=0;m<powerOf10;m++)loops=loops*10;
GetLocalTime(&myTime);
Edit1->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeStart = myTime.wSecond*1000+myTime.wMilliseconds;
if (RBtnInteger->Checked)
for(n=0; n<loops; n= n+1) ;
if (RBtnFloat->Checked)
for(n=0; n<loops; n= n+1) ;
GetLocalTime(&myTime);
Edit2->Text = IntToStr(myTime.wMinute)+':'+
IntToStr(myTime.wSecond)+':'+
IntToStr(myTime.wMilliseconds);
timeEnd = myTime.wSecond*1000+myTime.wMilliseconds;
Edit3->Text=IntToStr(timeEnd-timeStart);
Edit4->Text=FloatToStrF((timeEnd-timeStart)*1e6/loops,ffExponent,3,2);
}
//---------------------------------------------------------------------------
Delphi
Code:
unit time06;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BtnAdd: TButton;
BtnSubtract: TButton;
BtnMultiply: TButton;
BtnDivide: TButton;
RGrpType: TRadioGroup;
RBtnInteger: TRadioButton;
RBtnReal: TRadioButton;
EdtPowerOfTen: TEdit;
Label4: TLabel;
Edit4: TEdit;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
BtnEmpty: TButton;
procedure Button1Click(Sender: TObject);
procedure BtnAddClick(Sender: TObject);
procedure BtnSubtractClick(Sender: TObject);
procedure BtnMultiplyClick(Sender: TObject);
procedure BtnDivideClick(Sender: TObject);
procedure RBtnIntegerClick(Sender: TObject);
procedure RBtnRealClick(Sender: TObject);
procedure BtnEmptyClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
h, m, s, hund : Word;
a, b, c : Real;
startTime, endTime : LongInt;
x, y, z : Integer;
implementation
{$R *.DFM}
// allow timing of selected PC activities
// modified 23 Nov 2005
procedure TForm1.Button1Click(Sender: TObject);
var myTime: SYSTEMTIME;
i, PowerOfTen : integer;
n,loops : LongInt;
begin
PowerOfTen := StrToInt(EdtpowerOfTen.Text);
loops := 1;
for i:=1 to PowerOfTen do loops := loops*10;
getsystemtime(myTime); // get time from operating system
startTime := myTime.wminute*60000 + myTime.wSecond*1000 + myTime.wMilliseconds;
Edit1.Text := InttoStr(MyTime.wminute) + ':' + InttoStr(myTime.wsecond) +
'.'+ InttoStr(myTime.wMilliseconds);
a := 0.5; b := 1.1; c := 2.2;
x := 1; y := 2;
for n := 1 to loops do // see lab sheet for suggested n values
begin
// z := x + y ; // Integer arithmetic
// z := x div y ;
a := b + c ; // Real arithmetic
a := b/c ;
// a := cos(b);
// a := ln(c);
// Form1.Caption := InttoStr(n); //edit these out when doing arithmetic tests
end;
getsystemtime(myTime);
endTime := myTime.wminute*60000 + myTime.wsecond*1000 + myTime.wMilliseconds;
Edit2.Text := InttoStr(myTime.wminute)+':'+ InttoStr(myTime.wsecond) +
':'+ InttoStr(myTime.wMilliseconds);
Edit3.Text := InttoStr( EndTime - startTime);
end;
procedure TForm1.BtnAddClick(Sender: TObject);
var myTime: SYSTEMTIME;
i, PowerOfTen : integer;
n,loops : LongInt;
begin
PowerOfTen := StrToInt(EdtpowerOfTen.Text);
loops := 1;
for i:=1 to PowerOfTen do loops := loops*10;
getsystemtime(myTime); // get time from operating system
startTime := myTime.wminute*60000 + myTime.wSecond*1000 + myTime.wMilliseconds;
Edit1.Text := InttoStr(MyTime.wminute) + ':' + InttoStr(myTime.wsecond) +
'.'+ InttoStr(myTime.wMilliseconds);
x := 1; y := 2; b:= 3.0; c := 5.0;
if RBtnInteger.Checked then
for n := 1 to loops do z := x + y ; // Integer add
if RBtnReal.Checked then
for n := 1 to loops do a := b + c ; // Real add
getsystemtime(myTime);
endTime := myTime.wminute*60000 + myTime.wsecond*1000 + myTime.wMilliseconds;
Edit2.Text := InttoStr(myTime.wminute)+':'+ InttoStr(myTime.wsecond) +
':'+ InttoStr(myTime.wMilliseconds);
Edit3.Text := InttoStr( EndTime - startTime);
Edit4.Text := FloatToStrF(((EndTime - startTime)*1e6)/loops,ffExponent,3,8) ;
end;
procedure TForm1.BtnSubtractClick(Sender: TObject);
var myTime: SYSTEMTIME;
i, PowerOfTen : integer;
n,loops : LongInt;
begin
PowerOfTen := StrToInt(EdtpowerOfTen.Text);
loops := 1;
for i:=1 to PowerOfTen do loops := loops*10;
getsystemtime(myTime); // get time from operating system
startTime := myTime.wminute*60000 + myTime.wSecond*1000 + myTime.wMilliseconds;
Edit1.Text := InttoStr(MyTime.wminute) + ':' + InttoStr(myTime.wsecond) +
'.'+ InttoStr(myTime.wMilliseconds);
x := 1; y := 2; b:= 3.0; c := 5.0;
if RBtnInteger.Checked then
for n := 1 to loops do z := x - y ; // Integer add
if RBtnReal.Checked then
for n := 1 to loops do a := b - c ; // Real add
getsystemtime(myTime);
endTime := myTime.wminute*60000 + myTime.wsecond*1000 + myTime.wMilliseconds;
Edit2.Text := InttoStr(myTime.wminute)+':'+ InttoStr(myTime.wsecond) +
':'+ InttoStr(myTime.wMilliseconds);
Edit3.Text := InttoStr( EndTime - startTime);
Edit4.Text := FloatToStrF(((EndTime - startTime)*1e6)/loops,ffExponent,3,8) ;
end;
procedure TForm1.BtnMultiplyClick(Sender: TObject);
var myTime: SYSTEMTIME;
i, PowerOfTen : integer;
n,loops : LongInt;
begin
PowerOfTen := StrToInt(EdtpowerOfTen.Text);
loops := 1;
for i:=1 to PowerOfTen do loops := loops*10;
getsystemtime(myTime); // get time from operating system
startTime := myTime.wminute*60000 + myTime.wSecond*1000 + myTime.wMilliseconds;
Edit1.Text := InttoStr(MyTime.wminute) + ':' + InttoStr(myTime.wsecond) +
'.'+ InttoStr(myTime.wMilliseconds);
x := 1; y := 2; b:= 3.0; c := 5.0;
if RBtnInteger.Checked then
for n := 1 to loops do z := x * y ; // Integer add
if RBtnReal.Checked then
for n := 1 to loops do a := b * c ; // Real add
getsystemtime(myTime);
endTime := myTime.wminute*60000 + myTime.wsecond*1000 + myTime.wMilliseconds;
Edit2.Text := InttoStr(myTime.wminute)+':'+ InttoStr(myTime.wsecond) +
':'+ InttoStr(myTime.wMilliseconds);
Edit3.Text := InttoStr( EndTime - startTime);
Edit4.Text := FloatToStrF(((EndTime - startTime)*1e6)/loops,ffExponent,3,8) ;
end;
procedure TForm1.BtnDivideClick(Sender: TObject);
var myTime: SYSTEMTIME;
i, PowerOfTen : integer;
n,loops : LongInt;
begin
PowerOfTen := StrToInt(EdtpowerOfTen.Text);
loops := 1;
for i:=1 to PowerOfTen do loops := loops*10;
getsystemtime(myTime); // get time from operating system
startTime := myTime.wminute*60000 + myTime.wSecond*1000 + myTime.wMilliseconds;
Edit1.Text := InttoStr(MyTime.wminute) + ':' + InttoStr(myTime.wsecond) +
'.'+ InttoStr(myTime.wMilliseconds);
x := 1; y := 2; b:= 3.0; c := 5.0;
if RBtnInteger.Checked then
for n := 1 to loops do z := x div y ; // Integer divide
if RBtnReal.Checked then
for n := 1 to loops do
a := b/c ; // Real divide
getsystemtime(myTime);
endTime := myTime.wminute*60000 + myTime.wsecond*1000 + myTime.wMilliseconds;
Edit2.Text := InttoStr(myTime.wminute)+':'+ InttoStr(myTime.wsecond) +
':'+ InttoStr(myTime.wMilliseconds);
Edit3.Text := InttoStr( EndTime - startTime);
Edit4.Text := FloatToStrF(((EndTime - startTime)*1e6)/loops,ffExponent,3,8) ;
end;
procedure TForm1.RBtnIntegerClick(Sender: TObject);
begin
BtnDivide.Caption := 'div';
end;
procedure TForm1.RBtnRealClick(Sender: TObject);
begin
BtnDivide.Caption := '\';
end;
procedure TForm1.BtnEmptyClick(Sender: TObject);
var myTime: SYSTEMTIME;
i, PowerOfTen : integer;
n,loops : LongInt;
begin
PowerOfTen := StrToInt(EdtpowerOfTen.Text);
loops := 1;
for i:=1 to PowerOfTen do loops := loops*10;
getsystemtime(myTime); // get time from operating system
startTime := myTime.wminute*60000 + myTime.wSecond*1000 + myTime.wMilliseconds;
Edit1.Text := InttoStr(MyTime.wminute) + ':' + InttoStr(myTime.wsecond) +
'.'+ InttoStr(myTime.wMilliseconds);
x := 1; y := 2; b:= 3.0; c := 5.0;
if RBtnInteger.Checked then
for n := 1 to loops do ;
if RBtnReal.Checked then
for n := 1 to loops do ;
getsystemtime(myTime);
endTime := myTime.wminute*60000 + myTime.wsecond*1000 + myTime.wMilliseconds;
Edit2.Text := InttoStr(myTime.wminute)+':'+ InttoStr(myTime.wsecond) +
':'+ InttoStr(myTime.wMilliseconds);
Edit3.Text := InttoStr( EndTime - startTime);
Edit4.Text := FloatToStrF(((EndTime - startTime)*1e6)/loops,ffExponent,3,8) ;
end;
end.
If you could provide any insight at all i'd be greatful.