You are not logged in. Please login at www.codechef.com to post your questions!

×

Why Such difference in Execution Time?

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)

asked 25 Nov '18, 17:24

prembharwani's gravatar image

2★prembharwani
195
accept rate: 0%

edited 25 Nov '18, 17:53


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

link

answered 25 Nov '18, 17:56

admin5's gravatar image

5★admin5
2269
accept rate: 18%

edited 25 Nov '18, 17:58

Oh thanks !

@admin5 so using "\n" doesn't do the same?

(25 Nov '18, 18:00) prembharwani2★
1

Yeah! because \n just print the next line nothing else

(25 Nov '18, 18:10) admin55★

For what its worth, this code is definitely faster:

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

The contributing factors:

link

answered 25 Nov '18, 17:57

oleg_b's gravatar image

7★oleg_b
2174
accept rate: 18%

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 !

(25 Nov '18, 18:04) prembharwani2★

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

(25 Nov '18, 18:22) admin55★

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.

link

answered 25 Nov '18, 17:39

electricgeek's gravatar image

0★electricgeek
1
accept rate: 0%

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

(25 Nov '18, 17:40) prembharwani2★
2

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.

(25 Nov '18, 18:40) vijju123 ♦♦4★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,901
×74
×21
×20

question asked: 25 Nov '18, 17:24

question was seen: 168 times

last updated: 25 Nov '18, 18:40