- Commit
- e0933798b0fdbae94bf4fe49334e47920dbf74fd
- Parent
- 475301d7e02ab1acc3736b3095c40188e6dfa47c
- Author
- Pablo Emilio Escobar Gaviria <pablo-escobar@riseup.net>
- Date
Benchmarked the C++ implementation
An exercise on polyglossy: the same problem solved on multiple languages
Benchmarked the C++ implementation
4 files changed, 15 insertions, 18 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | Makefile | 4 | 4 | 0 |
Modified | c++/main.cpp | 6 | 3 | 3 |
Modified | c/main.c | 14 | 5 | 9 |
Modified | stats.csv | 9 | 3 | 6 |
diff --git a/Makefile b/Makefile @@ -1,6 +1,9 @@ c-bin: gcc -O2 -pthread -o bin/c ./c/main.c +cpp-bin: + g++ -O2 -o ./bin/cpp -std=c++11 -pthread ./c++/main.cpp + elixir-bin: elixirc -o bin/elixir ./elixir/main.ex @@ -26,6 +29,7 @@ ocaml-bin: ocamlopt \ -thread \ -package threads \ + -nodynlink \ -o ./bin/ocaml \ -linkpkg ./ocaml/main.ml \ && rm ./ocaml/*.cmi ./ocaml/*.cmx ./ocaml/*.o
diff --git a/c++/main.cpp b/c++/main.cpp @@ -1,10 +1,8 @@ -#include <chrono> #include <iostream> #include <string> #include <thread> #include <map> #include <vector> -//#include <charconv> #define SUCCESS 0 #define FAIL 1 @@ -24,7 +22,9 @@ inline unsigned int sum_digits(unsigned int n) return sum; } -void get_counterexpl_thread(unsigned int start, unsigned int max, unsigned int interval) +void get_counterexpl_thread(unsigned int start, + unsigned int max, + unsigned int interval) { for (auto a = start; a <= max; a += interval) for (auto b = a; b <= max; b++)
diff --git a/c/main.c b/c/main.c @@ -64,17 +64,13 @@ int counterexempl(range *r) int main(int argc, char *argv[]) { - if (argc > 1) + if (argc > 2) { unsigned int max = strtoul(argv[1], NULL, 10); - if (!max) return INVALID_INPUT; + if (!max) return INVALID_INPUT; - unsigned int n_threads = 1; - if (argc > 2) - { - n_threads = strtoul(argv[2], NULL, 10); - if (!n_threads) return INVALID_INPUT; - } + unsigned int n_threads = strtoul(argv[2], NULL, 10); + if (!n_threads) return INVALID_INPUT; pthread_t thread_ids[n_threads]; range thread_ranges[n_threads]; @@ -88,7 +84,7 @@ int main(int argc, char *argv[]) // Divide the task into the specified number of threads for (int i = 0; i < n_threads; i++) { - thread_ranges[i] = (range){i, n_threads, max}; + thread_ranges[i] = (range){i, n_threads, max}; err = pthread_create( &thread_ids[i], NULL,