Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <thread>
- #include <mutex>
- #include <functional>
- #include <cstdlib>
- #include <chrono>
- #include <ctime>
- #include <boost/asio/io_service.hpp>
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- int main()
- {
- const int NUM_ACCESSES = 400000;
- const int NUM_ELEMENTS = 100;
- const int NUM_THREADS = 4;
- const int NUM_RUNS = 25;
- srand(time(NULL));
- std::vector<int> test(NUM_ELEMENTS, 0);
- std::mutex test_mutexes[NUM_ELEMENTS];
- std::mutex share;
- int average = 0;
- std::function<void()> func1 = [&]()
- {
- int index;
- for(unsigned int i = 0; i < NUM_ACCESSES/NUM_THREADS; i++)
- {
- index = rand() % NUM_ELEMENTS;
- test_mutexes[index].lock();
- test[index] = rand();
- test_mutexes[index].unlock();
- }
- };
- std::function<void()> func2 = [&]()
- {
- int index;
- for(unsigned int i = 0; i < NUM_ACCESSES/NUM_THREADS; i++)
- {
- index = rand() % NUM_ELEMENTS;
- share.lock();
- test[index] = rand();
- share.unlock();
- }
- };
- std::cout << "Non-Concurrent Control Test:\n";
- for(int i = 0; i < NUM_RUNS; i++)
- {
- std::chrono::time_point<std::chrono::steady_clock> start, end;
- start = std::chrono::steady_clock::now();
- for(int i = 0; i < NUM_ACCESSES; i++)
- {
- int index = rand() % NUM_ELEMENTS;
- test[index] = rand();
- }
- end = std::chrono::steady_clock::now();
- auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
- std::cout << "Elapsed time: " << elapse.count() << "ms\n";
- average += elapse.count();
- }
- std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
- std::cout << "Individual Mutex Implement Test:\n";
- average = 0;
- for(int i = 0; i < NUM_RUNS; i++)
- {
- std::chrono::time_point<std::chrono::steady_clock> start, end;
- boost::thread threads[NUM_THREADS] = boost::thread(func1);
- start = std::chrono::steady_clock::now();
- for(auto& thread: threads )
- thread.join();
- end = std::chrono::steady_clock::now();
- auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
- std::cout << "Elapsed time: " << elapse.count() << "ms\n";
- average += elapse.count();
- }
- std::cout << "Average time: " << average/NUM_RUNS << "ms\n";
- average = 0;
- std::cout << "\n";
- std::cout << "Shared Mutex Implement Test:\n";
- for(int i = 0; i < NUM_RUNS; i++)
- {
- std::chrono::time_point<std::chrono::steady_clock> start, end;
- boost::thread threads[NUM_THREADS] = boost::thread(func2);
- start = std::chrono::steady_clock::now();
- for(auto& thread: threads )
- thread.join();
- end = std::chrono::steady_clock::now();
- auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
- std::cout << "Elapsed time: " << elapse.count() << "ms\n";
- average += elapse.count();
- }
- std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
- std::cout << "boost threadpool test on individual mutexes\n";
- average = 0;
- boost::asio::io_service ioService;
- boost::thread_group threadpool;
- std::function<void()> func3 = [&]()
- {
- int index = rand() % NUM_ELEMENTS;
- test_mutexes[index].lock();
- test[index] = rand();
- test_mutexes[index].unlock();
- };
- boost::asio::io_service::work work(ioService);
- for(int i = 0; i < 4; i++)
- boost::bind(&boost::asio::io_service::run, &ioService);
- for(int i = 0; i < NUM_RUNS; i++)
- {
- std::chrono::time_point<std::chrono::steady_clock> start, end;
- start = std::chrono::steady_clock::now();
- for(int i = 0; i < NUM_ACCESSES; i++)
- {
- ioService.post(boost::bind(func3));
- }
- threadpool.join_all();
- end = std::chrono::steady_clock::now();
- auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
- std::cout << "Elapsed time: " << elapse.count() << "ms\n";
- average += elapse.count();
- }
- std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
- std::cout << "boost threadpool test on a shared mutex\n";
- average = 0;
- std::function <void()> func4 =[&]()
- {
- int index = rand() & NUM_ELEMENTS;
- share.lock();
- test[index] = rand();
- share.unlock();
- };
- for(int i = 0; i < NUM_RUNS; i++)
- {
- std::chrono::time_point<std::chrono::steady_clock> start, end;
- start = std::chrono::steady_clock::now();
- for(int i = 0; i < NUM_ACCESSES; i++)
- ioService.post(boost::bind(func4));
- threadpool.join_all();
- end = std::chrono::steady_clock::now();
- auto elapse = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
- std::cout << "Elapsed time: " << elapse.count() << "ms\n";
- average += elapse.count();
- }
- std::cout << "Average time: " << average/NUM_RUNS << "ms\n\n";
- ioService.stop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement