简体   繁体   中英

Why does replacing long long with int increase performance?

Some tasks on Codeforces result in a "TLE" (Time Limit Exceeded) when using a long long for some variables, while changing them to int results in an "Accepted".

How does that issue affect the code? How do compilers deal with it? Why is the code faster when using int ?

This depends heavily on the platform. Here are two instances of where using a long long may slow down your code:

  1. If the CPU is not 64 bit (assuming that long long is 64 bit and int is 32 bit) then there is more work involved in performing operations such as addition.
  2. If you are working on a lot of data, changing from an int to a long long can have a large impact because the data is twice as large. Pulling data from disk or ram to the CPU cache is expensive, and if the datatype is twice as big, the CPU must request data more often.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM