Skip to content
Snippets Groups Projects
Commit 818aa561 authored by samarth5's avatar samarth5
Browse files

Adding gitignore; Making benchmarking code more generic

parent f9fa33da
No related branches found
No related tags found
No related merge requests found
*.o
*.csv
BenchmarkingModule
\ No newline at end of file
...@@ -4,31 +4,36 @@ ...@@ -4,31 +4,36 @@
#include <numeric> #include <numeric>
#include <thread> #include <thread>
#include <assert.h> #include <assert.h>
#include <cxxabi.h>
using namespace std; using namespace std;
using namespace std::chrono; using namespace std::chrono;
#define quote(x) #x
const int NumberToEnqueue = 2;
template <class T> template <class T>
void EnqueueNItems(IQueue<T> &queue, int n) void EnqueueNItems(IQueue<T> &queue, int n)
{ {
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
queue.enqueue(2); queue.enqueue(NumberToEnqueue);
} }
} }
double CreateNThreadsAndPerformKOperations(int n, int k) template <class T>
void CreateNThreadsAndPerformKOperations(string operatioName, void (*OperationToPerform)(IQueue<T>
&queueInstance, int numberOfRepititions), IQueue<T> *baseQueuePointer, int n, int k)
{ {
IQueue<T> & baseQueue = *baseQueuePointer;
thread workers[n]; thread workers[n];
LockQueue<int> lockedQueue;
auto start = high_resolution_clock::now(); auto start = high_resolution_clock::now();
// do some work
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
workers[i] = thread(EnqueueNItems<int>, ref(lockedQueue), k); workers[i] = thread(OperationToPerform, ref(baseQueue), k);
} }
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
...@@ -37,15 +42,25 @@ double CreateNThreadsAndPerformKOperations(int n, int k) ...@@ -37,15 +42,25 @@ double CreateNThreadsAndPerformKOperations(int n, int k)
} }
auto end = high_resolution_clock::now(); auto end = high_resolution_clock::now();
duration<double> diff = end-start;
assert(lockedQueue.size() == n*k);
assert(baseQueue.size() == n*k);
duration<double> diff = end-start; int status;
return diff.count(); char * demangledName = abi::__cxa_demangle(typeid(baseQueue).name(), 0, 0, &status);
printf("%s,%s,%f,%d,%d\n", demangledName, operatioName.c_str(), diff.count(), n, k);
} }
// 1. Try with different types of objects (ints, classes) to see if that affects the performance.
// 2. Try only enqueue operations.
// 3. Try enqueue and dequeue operations together.
int main() int main()
{ {
cout<<CreateNThreadsAndPerformKOperations(100, 1000)<<endl; cout<<"Queue,OperationName,TimeTakenToExecute,NumberOfThreads,NumberOfOperations"<<endl;
cout<<CreateNThreadsAndPerformKOperations(100, 100000)<<endl; CreateNThreadsAndPerformKOperations(quote(EnqueueNItems), EnqueueNItems, new LockQueue<int>(),
100, 1000);
CreateNThreadsAndPerformKOperations(quote(EnqueueNItems), EnqueueNItems, new LockQueue<int>(),
100, 100000);
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment