mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Janitor : Add performance test for string to int conversion
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "RiaStdStringTools.h"
|
||||
|
||||
#include <QLocale>
|
||||
#include <charconv>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
@@ -57,3 +58,59 @@ TEST( RiaStdStringToolsTest, TrimStrings )
|
||||
EXPECT_EQ( text, expectedText );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RiaStdStringToolsTest, DISABLED_PerformanceConversion )
|
||||
{
|
||||
size_t itemCount = 1000000;
|
||||
std::string valueAsString = "1234567";
|
||||
|
||||
int intValue = 0;
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
for ( size_t i = 0; i < itemCount; i++ )
|
||||
{
|
||||
intValue = std::stoi( valueAsString );
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> diff = end - start;
|
||||
std::cout << "std::stoi (no try/catch) " << std::setw( 9 ) << diff.count() << " s\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
for ( size_t i = 0; i < itemCount; i++ )
|
||||
{
|
||||
try
|
||||
{
|
||||
intValue = std::stoi( valueAsString );
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> diff = end - start;
|
||||
std::cout << "std::stoi incl try/catch" << std::setw( 9 ) << diff.count() << " s\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
for ( size_t i = 0; i < itemCount; i++ )
|
||||
{
|
||||
std::from_chars( valueAsString.data(), valueAsString.data() + valueAsString.size(), intValue );
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> diff = end - start;
|
||||
std::cout << "std::from_chars " << std::setw( 9 ) << diff.count() << " s\n";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user