Last active
December 9, 2021 17:44
-
-
Save DennisPing/37cd5c9276ce2dde4aacc20af925f0fe to your computer and use it in GitHub Desktop.
C++ std async vs regular loops performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tutorial by The Cherno on Youtube | |
// https://youtu.be/5HWCsmE9DrE | |
// Compile using | |
// g++ -std=c++17 fast.cpp -o fast | |
#include <vector> | |
#include <chrono> | |
#include <thread> | |
#include <future> | |
#include <iostream> | |
#include <cmath> | |
std::vector<double> repetitive_math() { | |
std::vector<double> subvector; | |
double result = 0; | |
for (double i = pow(5,7); i >= 0; i--) { | |
result += atan(i) * tan(i); | |
subvector.push_back(result); | |
} | |
return subvector; | |
} | |
int main() { | |
using std::chrono::high_resolution_clock; | |
using std::chrono::duration_cast; | |
using std::chrono::duration; | |
using std::chrono::milliseconds; | |
std::vector<std::future<std::vector<double>>> futures; | |
auto start = high_resolution_clock::now(); | |
// The main task | |
for (int i=0; i<500; i++) { | |
futures.push_back(std::async(std::launch::async, repetitive_math)); | |
} | |
auto end = high_resolution_clock::now(); | |
auto ms_int = duration_cast<milliseconds>(end - start); | |
std::cout << "Duration: " << ms_int.count() << " ms" << std::endl; | |
std::cout << "The size of the outer vector: " << futures.size() << std::endl; | |
std::cout << "The size of the inner vector: " << futures[1].get().size() << std::endl; | |
return 0; | |
} | |
// Result | |
// Duration: 391 ms | |
// The size of the outer vector: 500 | |
// The size of the inner vector: 78126 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile using | |
// g++ -std=c++17 slow.cpp -o slow | |
#include <vector> | |
#include <chrono> | |
#include <thread> | |
#include <future> | |
#include <iostream> | |
#include <cmath> | |
std::vector<double> repetitive_math() { | |
std::vector<double> subvector; | |
double result = 0; | |
for (double i = pow(5,7); i >= 0; i--) { | |
result += atan(i) * tan(i); | |
subvector.push_back(result); | |
} | |
return subvector; | |
} | |
int main() { | |
using std::chrono::high_resolution_clock; | |
using std::chrono::duration_cast; | |
using std::chrono::duration; | |
using std::chrono::milliseconds; | |
// Instead of using futures, we are using a regular 2d vector | |
std::vector<std::vector<double>> multivector; | |
auto start = high_resolution_clock::now(); | |
// The main task | |
for (int i=0; i<500; i++) { | |
multivector.push_back(repetitive_math()); | |
} | |
auto end = high_resolution_clock::now(); | |
auto ms_int = duration_cast<milliseconds>(end - start); | |
std::cout << "Duration: " << ms_int.count() << " ms" << std::endl; | |
std::cout << "The size of the outer vector: " << multivector.size() << std::endl; | |
std::cout << "The size of the inner vector: " << multivector[1].size() << std::endl; | |
return 0; | |
} | |
// Result | |
// Duration: 3273 ms | |
// The size of the outer vector: 500 | |
// The size of the inner vector: 78126 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment