Reputation: 570
I wanted to see which would access faster, a struct or a tuple, so I wrote a small program. However, when it finishes running the time recorded is 0.000000 for both. I'm pretty sure the program isn't finishing that fast(running on an online compiler since I am away from home)
#include <iostream>
#include <time.h>
#include <tuple>
#include <cstdlib>
using namespace std;
struct Item
{
int x;
int y;
};
typedef tuple<int, int> dTuple;
int main() {
printf("Let's see which is faster...\n");
//Timers
time_t startTime;
time_t currentTimeTuple;
time_t currentTimeItem;
//Delta times
double deltaTimeTuple;
double deltaTimeItem;
//Collections
dTuple tupleArray[100000];
struct Item itemArray[100000];
//Seed random number
srand(time(NULL));
printf("Generating tuple array...\n");
//Initialize an array of tuples with random ints
for(int i = 0; i < 100000; ++i)
{
tupleArray[i] = dTuple(rand() % 1000,rand() % 1000);
}
printf("Generating Item array...\n");
//Initialize an array of Items
for(int i = 0; i < 100000; ++i)
{
itemArray[i].x = rand() % 1000;
itemArray[i].y = rand() % 1000;
}
//Begin timer for tuple array
time(&startTime);
//Iterate through the array of tuples and print out each value, timing how long it takes
for(int i = 0; i < 100000; ++i)
{
printf("%d: %d", get<0>(tupleArray[i]), get<1>(tupleArray[i]));
}
//Get the time it took to go through the tuple array
time(¤tTimeTuple);
deltaTimeTuple = difftime(startTime, currentTimeTuple);
//Start the timer for the array of Items
time(&startTime);
//Iterate through the array of Items and print out each value, timing how long it takes
for(int i = 0; i < 100000; ++i)
{
printf("%d: %d", itemArray[i].x, itemArray[i].y);
}
//Get the time it took to go through the item array
time(¤tTimeItem);
deltaTimeItem = difftime(startTime, currentTimeItem);
printf("\n\n");
printf("It took %f seconds to go through the tuple array\nIt took %f seconds to go through the struct Item array\n", deltaTimeTuple, deltaTimeItem);
return 0;
}
According to www.cplusplus.com/reference/ctime/time/, difftime should return the difference between two time_t.
Upvotes: 0
Views: 71
Reputation: 387
time_t
is a count of seconds, and 100,000 trivial operations on a fast, modern computer could indeed finish in less than a second.
Upvotes: 0
Reputation: 73376
time() generally returns the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). So depending on your library implementation, anything below 1 second might appear as 0.
You should prefer use of <chrono>
for benchmarking purpose:
chrono::high_resolution_clock::time_point t;
t = high_resolution_clock::now();
// do something to benchmark
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
cout <<"Exec in ms: "<< chrono::duration_cast<milliseconds>(t2 - t).count() <<endl;
You have nevertheless to consider the clock resolution. For example with windows it's 15 ms, so if you are close around 15 ms, or even below, you should increase the number of iterations.
Upvotes: 2
Reputation: 57698
I recommend checking the assembly language listing before you start performance timings.
The first thing to check is that the assembly language is significantly different between accessing a tuple
versus accessing your structure. You can get a rough estimate of the timing difference by counting the number of different instructions.
Secondly, I recommend looking at the definition of Tuple
. Something tells me that it is a structure declared similarly to yours. Thus I predict your timings should be near equal.
Thirdly, you should also compare std::pair
since you only have 2 items in your tuple
and a std::pair
has two elements. Again, it should be the same as your structure.
Lastly, be prepared for "noise" in your data. The noise may come from the data cache misses, other programs using the data cache, delegation of code between cores, and any I/O. The closer you can get to running your program on a single core, free of interruptions, the better quality your data will be.
Upvotes: 1