Why Such difference in Execution Time?

c-plus-plus
execution-time
i_hate_error
i_hate_errors

#1

Execution Time increases by 0.96 secs just because I replaced the data type of a variable from “long long int” to "int" . I was wondering why this happens ? Please Note ( long long takes lesser time than just int ; this is what i found weird)

Problem: https://www.codechef.com/problems/TSORT

Solutions 1) https://www.codechef.com/viewsolution/21700485 (used long long here)

           2)   https://www.codechef.com/viewsolution/21700483    (used just int here)

#2

sizeof(int) is 16 bits(minimum possible in C language) where as long int is 32 bits and long long int is 64 bit here the space required for the execution of the code will increase exponentially(ie 2^n).
Since the space required has increased,

=>more data has to be carried in between RAM and processor,

=>since finite bits of data can be transferred per cycle from ram to processor => more cycles,
(varies from processor to processor)

=>each cycle has some execution time.

Hence, the increase in execution time.


#3

Execution time is not increased by data type, it is just because of endl

endl flush the cache or buffer whenever it executes, that is why it increases the execution of your solution


#4

For what its worth, this code is definitely faster:

#include
using namespace std;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t, n;
  cin>>t;
  vector a(1000001, 0);
  for(int i=0; i> n;
      ++a[n];
  }
  for(int i=0; i<=1000000; ++i) {
      for(int j=0; j<a*; ++j) {
            cout << i << "
";
      }
  }
  return 0;	       	     
}

The contributing factors:


#5

Sorry Dude
Rather the one with Long Long Is getting executed Earlier !


#6

Oh thanks !

@admin5 so using "
" doesn’t do the same?


#7

I’ll need to learn the first two factors you just mentioned !(it would be great if you could drop a link for the first one as well , Please)

Even Though @oleg_b thanks for such an Informative Answer ! I appreciate !


#8

Yeah! because
just print the next line nothing else


#9

First one is a trick to reduce the execution time of input and output statements this is called Fast IO.
Yeah but the real Fast IO is other than this.

You can find it on GeeksForGeeks called Fast IO


#10

sizeof(int) is 16 bits(minimum possible in C language) where as long int is 32 bits

int and long int have same size on codechef. 32 bits.