Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

vectors vs. lists timings

Hello,

I've written a program using vectors and the same one using lists.

My books says that for large inputs vectors run much slower than lists. I'd like to prove that by timing each program.

I thought of getting system time at the beginning and substract that from the system time at the end of the processing.

Can someone please advise how I can do that ? Are any other better suggestions to achieve that ?

Thanks.
[473 byte] By [ami] at [2007-11-11 10:08:22]
# 1 Re: vectors vs. lists timings
Hi,

You can use use the type and functions declared in the <time.h> header:

clock_t tstart;
clock_t tend;
clock_t total;

tstart = clock();

// do the stuff you want to assess
//...
//

tend = clock();

total = end - start;
// the total holds the timespan in milliseconds, but better check with your documentation

Cheers,

D
drkybelk at 2007-11-11 20:59:26 >
# 2 Re: vectors vs. lists timings
You can use the stopwatch class discussed in a former C++ 10 Minute Solution: http://www.dev-archive.com/dev-archive/LegacyLink/9478
Danny at 2007-11-11 21:00:27 >
# 3 Re: vectors vs. lists timings
this comes around from time to time. This is my assembly code for high resolution timer, its probably overkill but its great for things that clock cannot measure.
Also, try an array instead of either of the heavy hitting container objects, and you should see a major boost in speed.
jonnin at 2007-11-11 21:01:25 >
# 4 Re: vectors vs. lists timings
#ifndef jtimer_
#define jtimer_
#include<cstdio>

#define MHz 1000000 //one million hertz
#define machine_speed 1*MHz //your speed here!! //mine is set up for MS not seconds at this moment... adjust it for your system

namespace JtimerUsers
{
enum JtimerUsers_e
{
CpuMainLoop,
MaxUser //Reserved for use by the timer allocator
};
}

//void start_time(int dx);
//double elapsed_time(int dx);

//The version is using miliseconds. Some versions are in seconds.
void start_time(JtimerUsers::JtimerUsers_e eUser);
double elapsed_time(JtimerUsers::JtimerUsers_e eUser);
double average_time(JtimerUsers::JtimerUsers_e dx, int iterations);

#endif

_______________________________________ cpp file below, .h above
#include "Jtimer.h"

//High resolution timer via assembly language
//May fail on 64-bit windows, and is no good
//for non-windows (.net compiler) at all!

static __int64 jtimer[JtimerUsers::MaxUser];

void start_time(JtimerUsers::JtimerUsers_e dx)
{
if (dx < JtimerUsers::MaxUser)
{
// cpu instructions
#define rdtsc __asm __emit 0Fh __asm __emit 031h
#define cpuid __asm __emit 0Fh __asm __emit 0A2h

__int64 ts = 0; //working variable
__asm push EAX
__asm push EDX
//cpuid //other info
rdtsc //read time stamp register
__asm mov dword ptr ts, EAX //low bits
__asm and EDX, 07fffffffh //63 bit int, sign removed
__asm mov dword ptr ts+4,EDX //high bits
__asm pop EDX
__asm pop EAX
#undef rdtsc
#undef cpuid
jtimer[dx] = ts;
}
}

double elapsed_time(JtimerUsers::JtimerUsers_e eUser)
{
if (eUser < JtimerUsers::MaxUser)
{
#define rdtsc __asm __emit 0Fh __asm __emit 031h
#define cpuid __asm __emit 0Fh __asm __emit 0A2h
__int64 ts = 0;
__asm push EAX
__asm push EDX
//cpuid //other info
rdtsc //read time stamp register
__asm mov dword ptr ts, EAX //low bits
__asm and EDX, 07fffffffh //63 bit int, sign removed
__asm mov dword ptr ts+4,EDX //high bits
__asm pop EDX
__asm pop EAX
#undef rdtsc
#undef cpuid
ts -=jtimer[eUser];
return (double)(ts)/(double)(machine_speed);
}else{return -1.0;}
}

double average_time(JtimerUsers::JtimerUsers_e dx, int iterations)
{ /*
To use this one, you want to do a start time / average time pair in the loop
Note that you need to send this function 0,1,2,3,...N no matter how the loop
is performed. The following example maintains a variable to feed this routine
called 'lc' while looping over a sequence.

for(lc = 0, x = a, x < b; x+=3, lc++) //lc is just for the average time function!
{
start_time(1);
...
at = average_time(1,lc); //uses the same timer ID# as start_time call
}
'at' now has the average time for that loop...
etc...

note that You could use this to 'get' the time taken instead of elapsed_time which
prints a message -- for example
average_time(0,0);// reset the average
start_time(0);
code();
time = average_time(0,1); //report of 1 iteration
*/
static double total;
static double avg = 0;
if(iterations == 0)
{
total = 0;
return avg;
}
#define rdtsc __asm __emit 0Fh __asm __emit 031h
#define cpuid __asm __emit 0Fh __asm __emit 0A2h
__int64 ts = 0;
__asm push EAX
__asm push EDX
//cpuid //other info
//rdtsc //read time stamp register
__asm __emit 0Fh
__asm __emit 031h
__asm mov dword ptr ts, EAX //low bits
__asm and EDX, 07fffffffh //63 bit int, sign removed
__asm mov dword ptr ts+4,EDX //high bits
__asm pop EDX
__asm pop EAX
#undef rdtsc
#undef cpuid
ts -=jtimer[dx];
total += (double)(ts)/(double)(machine_speed);
avg = total/(double)iterations;
return avg;
}
jonnin at 2007-11-11 21:02:25 >