C++ 'unordered_map' VS C# 'Dictionary'
Ostatnio zmodyfikowano 2024-09-15 22:43
DejaVu Temat założony przez niniejszego użytkownika |
C++ 'unordered_map' VS C# 'Dictionary' » 2024-09-15 11:07:58 C++ source code (Visual Studio 2022, x64): #include <iostream> #include <unordered_map> #include <chrono>
int main() { std::unordered_map < int, int > map; auto start = std::chrono::high_resolution_clock::now(); for( int i = 0; i < 1000000; ++i ) map[ i ] = i; long long sum = 0; for( const auto & pair: map ) sum += pair.second; auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration < double > duration = end - start; std::cout << "Czas wykonania: " << duration.count() << " sekund" << std::endl; std::cout << "Suma wartości: " << sum << std::endl; return 0; }
C# source code (Visual Studio 2022, .NET 8, x64): using System; using System.Collections.Generic; using System.Diagnostics;
class Program { static void Main() { Dictionary<int, int> dictionary = new(); Stopwatch stopwatch = Stopwatch.StartNew(); for (int i = 0; i < 1000000; ++i) dictionary[i] = i;
long sum = 0; foreach (var kvp in dictionary) sum += kvp.Value;
stopwatch.Stop(); Console.WriteLine($"Czas wykonania: {stopwatch.Elapsed.TotalSeconds} sekund"); Console.WriteLine($"Suma wartości: {sum}"); } }
Custom C++ implementation (Visual Studio 2022, x64): #include <iostream> #include <chrono> #include <ddt/core/HashMap.hpp>
int main() { ddt::core::HashMap < int, int > map; auto start = std::chrono::high_resolution_clock::now(); for( int i = 0; i < 1000000; ++i ) map[ i ] = i; long long sum = 0; for( const auto & pair: map ) sum += pair.second; auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration < double > duration = end - start; std::cout << "Czas wykonania: " << duration.count() << " sekund" << std::endl; std::cout << "Suma wartości: " << sum << std::endl; return 0; }
All test results were made from Release builds:
Summary
C# Dictionary implementation is over 9 times faster than C++ std::unordered_map (Visual Studio 2022 libraries).Linux standard libraries are also much slower than C# standard implementation. |
|
DejaVu Temat założony przez niniejszego użytkownika |
» 2024-09-15 22:43:24 Nowszy benchmark
std::unordered_map, C++
#include <iostream> #include <unordered_map> #include <chrono>
int main() { auto start = std::chrono::high_resolution_clock::now(); auto count = 10000000; std::unordered_map < int, int > map; map.reserve( count ); for( int i = 0; i < count; ++i ) map[ i ] = i; long long sum = 0; for( const auto & pair: map ) sum += pair.second; auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration < double > duration = end - start; std::cout << "Czas wykonania: " << duration.count() << " sekund" << std::endl; std::cout << "Suma wartości: " << sum << std::endl; return 0; }
Wynik: Czas wykonania: 2.08319 sekund Suma wartości: 49999995000000
custom implementation, C++
#include <iostream> #include <chrono> #include <ddt/core/HashMap.hpp>
int main() { auto start = std::chrono::high_resolution_clock::now(); auto count = 10000000; ddt::core::HashMap < int, int > map; map.reserve( count ); for( int i = 0; i < count; ++i ) map[ i ] = i; long long sum = 0; for( const auto & pair: map ) sum += pair.second; auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration < double > duration = end - start; std::cout << "Czas wykonania: " << duration.count() << " sekund" << std::endl; std::cout << "Suma wartości: " << sum << std::endl; return 0; }
Wynik: Czas wykonania: 0.139059 sekund Suma wartości: 49999995000000
Dictionary, C# .NET 8
using System.Diagnostics;
class Program { static void Main() { Stopwatch stopwatch = Stopwatch.StartNew(); var count = 10000000; Dictionary<int, int> dictionary = new(count); for (int i = 0; i < count; ++i) dictionary[i] = i;
long sum = 0; foreach (var kvp in dictionary) sum += kvp.Value;
stopwatch.Stop(); Console.WriteLine($"Czas wykonania: {stopwatch.Elapsed.TotalSeconds} sekund"); Console.WriteLine($"Suma wartości: {sum}"); } }
Wynik: Czas wykonania: 0,1513187 sekund Suma wartości: 49999995000000
|
|
« 1 » |