About the operation speed of int and long long

Original link: https://blog.chungzh.cn/articles/int-vs-longlong/

foreword

When writing a CF question, the algorithm is obviously correct, but it is always TLE. Finally, an array of long long type was changed to int , and it turned out to be AC. .

This can’t help but lead me to think, is the operation speed of int and long long different?

Not rigorous testing

Since this chicken does not have any knowledge of the basic principles of computers, I had to do a test. Of course, this test is actually very imprecise and has no great reference value. I’m going to have fun too, hahahaha

test environment

  • Computer: Lenovo Yoga 14sACH 2021
  • System: Windows 11 25163.1010
  • CPU: AMD Ryzen 7 5800H with Radeon Graphics (16) @ 3.200GHz
  • RAM: 16.0 GB
  • Compiler: GCC 11.2.0

code

Just for the pleasure of the picture, I used Google Benchmark for the first time. It’s actually pretty good to start with.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 #include <benchmark/benchmark.h> using namespace benchmark ; static void int_add ( State & state ) { int a = std :: rand (), b = std :: rand (), c = 0 ; for ( auto _ : state ) DoNotOptimize ( c = ( ++ a ) + ( ++ b )); } static void ll_add ( State & state ) { long long a = std :: rand (), b = std :: rand (), c = 0 ; for ( auto _ : state ) DoNotOptimize ( c = ( ++ a ) + ( ++ b )); } static void int_div ( State & state ) { int a = std :: rand (), b = std :: rand (), c = 0 ; for ( auto _ : state ) DoNotOptimize ( c = ( ++ a ) / ( ++ b )); } static void ll_div ( State & state ) { long long a = std :: rand (), b = std :: rand (), c = 0 ; for ( auto _ : state ) DoNotOptimize ( c = ( ++ a ) / ( ++ b )); } static void int_mod ( State & state ) { int a = std :: rand (), b = std :: rand (), c = 0 ; for ( auto _ : state ) DoNotOptimize ( c = ( ++ a ) % ( ++ b )); } static void ll_mod ( State & state ) { long long a = std :: rand (), b = std :: rand (), c = 0 ; for ( auto _ : state ) DoNotOptimize ( c = ( ++ a ) % ( ++ b )); } BENCHMARK ( int_add ) -> Threads ( 8 ) -> Iterations ( 1e9 ); BENCHMARK ( ll_add ) -> Threads ( 8 ) -> Iterations ( 1e9 ); BENCHMARK ( int_div ) -> Threads ( 8 ) -> Iterations ( 1e9 ); BENCHMARK ( ll_div ) -> Threads ( 8 ) -> Iterations ( 1e9 ); BENCHMARK ( int_mod ) -> Threads ( 8 ) -> Iterations ( 1e9 ); BENCHMARK ( ll_mod ) -> Threads ( 8 ) -> Iterations ( 1e9 ); BENCHMARK_MAIN ();

result

 1 2 3 4 5 6 7 8 9 10
 --------------------------------------------------------------------- Benchmark Time CPU --------------------------------------------------------------------- int_add / iterations : 1000000000 / threads : 8 0 . 209 ns 1 . 57 ns ll_add / iterations : 1000000000 / threads : 8 0 . 225 ns 1 . 71 ns int_div / iterations : 1000000000 / threads : 8 0 . 302 ns 2 . 29 ns ll_div / iterations : 1000000000 / threads : 8 0 . 306 ns 2 . 38 ns int_mod / iterations : 1000000000 / threads : 8 0 . 345 ns 2 . 18 ns ll_mod / iterations : 1000000000 / threads : 8 0 . 350 ns 2 . 34 ns

After many tests, various operations of type long long are a little slower than int .

StackOverflow Q&A

A more professional answer. See performance – C++ int vs long long in 64 bit machine – Stack Overflow for details.

Here’s a related Q&A:

1) If it is best practice to use long long in x64 for achieving maximum performance even for for 1-4 byte data?

No- and it will probably in fact make your performance worse. For example, if you use 64-bit integers where you could have gotten away with 32-bit integers then you have just doubled the amount of data that must be sent between the processor and memory and the memory is orders of magnitude slower. All of your caches and memory buses will crap out twice as fast.

in conclusion

You can try not to use long long as much as possible. It’s best not to use the rudeness of #define int long long .


Thanks :

Sui Sui Nian: I haven’t written a blog for more than half a year, and the last update was during the winter vacation hahaha. The whole semester has been very busy, and the next is the third year of junior high.

?

This article is reproduced from: https://blog.chungzh.cn/articles/int-vs-longlong/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment