Karl said:
But it can be cured easily. Just tell the vector
how many integers it should expect
time(&start);
for ( tmp=0; tmp<SAMPLES; tmp++){
vector <int> intvector( SIZE );
for (int tmp2=0; tmp2<SIZE;tmp2++) intvector[tmp2]=tmp2;
for ( tmp2=0; tmp2<SIZE;tmp2++) tmpint=intvector[tmp2];
}
time(&stop);
With this modification, the allocated array and the vector are on a near
tie on my system.
Hi,
i investigated a little bit more time into this issue after your last
posting, because i also need a method to measure the efficiency of
sorting-algorithms
I had to rewrite your method a little bit for this test (vector resize).
I also took the architekturespecific rdtsc as timing reference, after i
was looking for a more precice method to catch the time. It might be not
standard conform from now on ... maybe [OT][OT]
Open issues: Huge Peaks -> out of process time-slice?
Regards marbac
#include <iostream>
#include <asm/msr.h> //not part of the standard ... found nothing
#include <vector>
using namespace std;
unsigned long int cyclecount() { //own time not eliminated
union {
unsigned int a[2];
unsigned long int b;
} tmp;
rdtsc(tmp.a[0],tmp.a[1]);
return tmp.b;
}
int main () {
int *intptr;
int tmpint;
vector <int> intvector;
unsigned long int start,stop;
for (int SIZE=1; SIZE<5000000; SIZE+=100000) {
start=cyclecount();
intptr=new int[SIZE];
for (int tmp2=0; tmp2<SIZE;tmp2++) intptr[tmp2]=tmp2;
for (int tmp2=0; tmp2<SIZE;tmp2++) tmpint=intptr[tmp2];
delete [] intptr;
stop=cyclecount();
cout << SIZE<<";" << stop-start << ";";
start=cyclecount();
for (int tmp2=0; tmp2<SIZE;tmp2++)
intvector.push_back(tmp2);
for (int tmp2=0; tmp2<SIZE;tmp2++)
tmpint=intvector[tmp2];
intvector.clear();
stop=cyclecount();
cout << stop-start << ";" ;
start=cyclecount();
intvector.resize( SIZE );
for (int tmp2=0; tmp2<SIZE;tmp2++) intvector[tmp2]=tmp2;
for (int tmp2=0; tmp2<SIZE;tmp2++)
tmpint=intvector[tmp2];
intvector.clear();
stop=cyclecount();
cout << stop-start << endl;
}
}
Results in CSV:
SIZE;PTRARRAY;VECPUSH;VEC[]
1;90240;33813;2654
100001;1896656;5664987;3146048
200001;4230510;9652769;6954714
300001;6560475;19215142;10414353
400001;8369995;8882148;15616197
500001;10558827;11020351;17199550
600001;12684491;37278433;20708852
700001;15086101;16791983;24092673
800001;17071504;19370898;28661935
900001;21307146;20523950;31445133
1000001;23156010;20607862;34606138
1100001;25174683;73625877;59363345
1200001;69786689;24653751;44094400
1300001;29101467;26656635;44958542
1400001;31332855;28733868;48584683
1500001;33332168;30702743;52032413
1600001;35405227;32930185;55892739
1700001;37700109;34945367;58622792
1800001;39462811;36891294;62915030
1900001;41600231;38950686;65648007
2000001;43695271;42831192;69223034
2100001;49232075;128685758;72172464
2200001;50027132;44577776;75539856
2300001;52210913;47116775;79139642
2400001;54281571;48872439;82872617
2500001;57062003;50617815;85639494
2600001;58712443;52762684;89109782
2700001;60791390;54667671;92389609
2800001;62956952;56679381;96426701
2900001;64791646;58617808;100038896
3000001;66694691;60873717;104918710
3100001;69668862;63589150;105669219
3200001;71590029;64368308;108763045
3300001;73443406;66443554;114306131
3400001;75971787;68211420;115523403
3500001;77243384;70734080;119151197
3600001;79104939;73109602;122264614
3700001;82405595;74260025;127104789
3800001;82316763;76214869;131076252
3900001;83774542;79485742;133650033
4000001;87420350;80137556;135647762
4100001;89002910;82514142;139303053
4200001;92127870;255667184;143994369
4300001;93281569;87342295;147955058
4400001;95212196;90756951;150916301
4500001;102373012;92316403;154436919
4600001;99994411;93167989;157996168
4700001;104611956;96599927;160801089
4800001;104253083;97598571;164166263
4900001;108748142;99228504;168168065