Click here to Skip to main content
13,837,307 members
Click here to Skip to main content
Add your own
alternative version

Stats

4.7K views
40 downloads
4 bookmarked
Posted 14 Jan 2019
Licenced CPOL

C++11 std::div() Benchmark

, 15 Jan 2019
Rate this:
Please Sign up or sign in to vote.
C++11 std::div() Benchmark

Update: rand() overhead in benchmark has been removed by filling the array with random values beforehand.

C++11 standard introduces std::div() and its siblings on the premise of some compiler can take advantage of the available machine code that compute quotient and remainder of division together. The C++ reference noted, and (updated) according to Petr Kobalíček, this function was never about performance but rounding direction of negative operands. We thank him for his comment.

Until C++11, the rounding direction of the quotient and the sign of the remainder in the built-in division and remainder operators was implementation-defined if either of the operands was negative, but it was well-defined in std::div.

On many platforms, a single CPU instruction obtains both the quotient and the remainder, and this function may leverage that, although compilers are generally able to merge nearby / and % where suitable.

We'll put std::div() to test in this benchmark.

Compiler Tested

  • GCC 7.4.0 on Cygwin
  • Clang 5.0.1 on Cygwin
  • Visual C++ 15.9 Update

OS: Windows 10 Pro

CPU: Intel i76820HQ

Loops: 10 million

Benchmark Code

stopwatch.start("Division and Modulus");
for (size_t i = 0; i < vec.size(); ++i)
{
    TwoNum& a = vec[i];
    result.quot = a.num / a.divisor;
    result.rem = a.num % a.divisor;
    total_result += result.quot + result.rem; // prevent optimize away
}
stopwatch.stop();

total_result = 0L;
stopwatch.start("Custom div function");
for (size_t i = 0; i < vec.size(); ++i)
{
    TwoNum& a = vec[i];
    result = my_div(a.num, a.divisor);
    total_result += result.quot + result.rem; // prevent optimize away
}
stopwatch.stop();

total_result = 0L;
stopwatch.start("std::div function");
for (size_t i = 0; i < vec.size(); ++i)
{
    TwoNum& a = vec[i];
    result = std::div(a.num, a.divisor);
    total_result += result.quot + result.rem; // prevent optimize away
}
stopwatch.stop();

Custom my_div() function is defined as below:

inline std::div_t my_div(int number, int divisior)
{
    return std::div_t{ (number / divisior), (number % divisior) };
}

Unoptimized Benchmark

GCC Unoptimized
Division and Modulus timing:108ms
Custom div function timing:150ms
std::div function timing:104ms

Clang Unoptimized
Division and Modulus timing:104ms
Custom div function timing:184ms
std::div function timing:102ms

VC++ Unoptimized
Division and Modulus timing:411ms
Custom div function timing:465ms
std::div function timing:427ms

On unoptimized GCC and Clang binary, std::div() is faster than my_div() and slightly faster than the individual division and modulus. For VC++, individual division and modulus is faster probably due to no overhead of function call.

Optimized Benchmark

GCC Optimized(O3)
Division and Modulus timing:30ms
Custom div function timing:34ms
std::div function timing:56ms

Clang Optimized(O3)
Division and Modulus timing:31ms
Custom div function timing:32ms
std::div function timing:54ms

VC++ Optimized(Ox)(Ot)
Division and Modulus timing:32ms
Custom div function timing:41ms
std::div function timing:50ms

On optimized binary, it is a shame that std::div() is consistently slower. In conclusion, today's compiler already does a very good job of computing division and modulus together without resorting to std::div() but it still has the performance lead when it comes to unoptimized build.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shao Voon Wong
Software Developer (Senior)
Singapore Singapore
Right now, I am picking up DevOps skills at Pluralsight and pursuing CCNA certification. Stay tuned for my CCNA related article!

Coding Tidbit Blog

Latest blogpost: C++ – The Forgotten Trojan Horse by Eric Johnson

IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • Certified Secure Software Lifecycle Professional (CSSLP)

View my certificates here.

You may also be interested in...

Comments and Discussions

 
Questionquotient and modulus together Pin
SeattleC++21-Jan-19 10:37
memberSeattleC++21-Jan-19 10:37 
Suggestionhints to make the benchmark better Pin
vickoza16-Jan-19 11:26
membervickoza16-Jan-19 11:26 
AnswerRe: hints to make the benchmark better Pin
Shao Voon Wong16-Jan-19 16:45
mvaShao Voon Wong16-Jan-19 16:45 
SuggestionDifferences in rand() implementation should be affecting too much Pin
armando bouza15-Jan-19 9:59
memberarmando bouza15-Jan-19 9:59 
AnswerRe: Differences in rand() implementation should be affecting too much Pin
Shao Voon Wong15-Jan-19 16:56
mvaShao Voon Wong15-Jan-19 16:56 
GeneralRe: Differences in rand() implementation should be affecting too much Pin
armando bouza15-Jan-19 23:57
memberarmando bouza15-Jan-19 23:57 
Questionwhy is vc++ so slow? Pin
Satervalley14-Jan-19 17:40
memberSatervalley14-Jan-19 17:40 
AnswerRe: why is vc++ so slow? Pin
Member 1052334715-Jan-19 6:38
memberMember 1052334715-Jan-19 6:38 
GeneralRe: why is vc++ so slow? Pin
Michael Waters15-Jan-19 8:12
memberMichael Waters15-Jan-19 8:12 
AnswerRe: why is vc++ so slow? Pin
Shao Voon Wong15-Jan-19 16:20
mvaShao Voon Wong15-Jan-19 16:20 
GeneralRe: why is vc++ so slow? Pin
degski15-Jan-19 20:21
memberdegski15-Jan-19 20:21 
GeneralRe: why is vc++ so slow? Pin
Shao Voon Wong16-Jan-19 14:51
mvaShao Voon Wong16-Jan-19 14:51 
GeneralRe: why is vc++ so slow? Pin
degski16-Jan-19 18:56
memberdegski16-Jan-19 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190114.1 | Last Updated 15 Jan 2019
Article Copyright 2019 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid