Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

C++ 'unordered_map' VS C# 'Dictionary'

Ostatnio zmodyfikowano 2024-09-15 22:43
Autor Wiadomość
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):
C/C++
#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):
C/C++
#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:
Test name Execution time Calculation result
C++, std::unordered_map 0.2128710s 499999500000
C#, Dictionary 0.0236454s 499999500000
C++, custom implementation 0.0220082s 499999500000

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.
P-181589
DejaVu
Temat założony przez niniejszego użytkownika
» 2024-09-15 22:43:24
Nowszy benchmark

std::unordered_map, C++

C/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++

C/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



P-181594
« 1 »
  Strona 1 z 1