Skip to content

Instantly share code, notes, and snippets.

@Rochet2
Last active January 30, 2016 17:12
Show Gist options
  • Save Rochet2/5f99f6f90d0f3f65735e to your computer and use it in GitHub Desktop.
Save Rochet2/5f99f6f90d0f3f65735e to your computer and use it in GitHub Desktop.
// Test solution2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <algorithm>
#include <assert.h>
#include <chrono>
#include <iostream>
#include <math.h>
#include <map>
#include <memory>
#include <mutex>
#include <random>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
typedef std::chrono::milliseconds resolution;
typedef std::chrono::time_point<std::chrono::system_clock> time_point;
static time_point start = std::chrono::system_clock::now();
static time_point end = std::chrono::system_clock::now();
inline void setstart()
{
start = std::chrono::system_clock::now();
}
inline void setend()
{
end = std::chrono::system_clock::now();
}
inline long long getdiff()
{
return std::chrono::duration_cast<resolution>(end - start).count();
}
inline void printtimeonly()
{
std::cout << getdiff() << std::endl;
}
inline void printtime()
{
setend();
printtimeonly();
setstart();
}
typedef uint64_t uint64;
typedef uint32_t uint32;
struct ELuNa
{
ELuNa() {}
ELuNa(const ELuNa& e) {}
typedef std::lock_guard<std::mutex> guard;
std::mutex lock;
std::vector<std::string> msgs;
};
void nop(ELuNa* e)
{
}
void MyThread1(ELuNa* e)
{
for (int i = 0; i < 100; ++i) {
ELuNa::guard g(e->lock);
e->msgs.push_back("mymessage");
}
}
void MyThread2(ELuNa* e)
{
for (int i = 0; i < 100; ++i) {
e->msgs.push_back("mymessage");
}
}
int main()
{
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<uint32> distevent(1, 50);
std::uniform_int_distribution<uint32> dist32(0u, std::numeric_limits<uint32>::max());
std::uniform_int_distribution<uint64> dist64(0u, std::numeric_limits<uint64>::max());
std::vector<long long> count;
count.reserve(110);
ELuNa globalE;
std::vector<ELuNa*> elunas;
for (int i = 0; i < 250; ++i)
elunas.push_back(new ELuNa());
for (int i = 0; i < 1000; ++i)
{
setstart();
{
//std::vector<std::thread*> threads;
//for (ELuNa* v : elunas) {
// threads.emplace_back(new std::thread(MyThread1, &globalE));
//}
//for (std::thread* v : threads) {
// v->join();
// delete v;
//}
//globalE.msgs.clear();
std::vector<std::thread*> threads;
for (ELuNa* v : elunas) {
threads.emplace_back(new std::thread(MyThread2, v));
}
for (std::thread* v : threads) {
v->join();
delete v;
}
for (ELuNa* v : elunas) {
v->msgs.clear();
}
}
setend();
count.emplace_back(getdiff());
}
long long sum = 0;
for (auto& v : count)
sum += v;
std::cout << sum / (double)count.size() << std::endl;
return 0;
}
@Rochet2
Copy link
Author

Rochet2 commented Jan 30, 2016

This code with the loops in MyThread1 and MyThread2 set to 100 iterations takes the same time for both approaches: locked queue and multiple lockless queues. (20 ms both approaches)
The locking starts to show only at 1000-10000 iterations in the loop. (30 ms lockless queues, 140 ms locked queue)

Changing the amount of threads does not seem to have a great effect. (thread count is equal to ELuNa count, so in this code it is set to 250 atm)
Tested with 1000 loop in MyThread functions and first at 200 threads and then at 1000 threads and the result was not that different. (first both at 20 ms with 200 threads and then 65 ms with 1000 threads)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment