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

×

Effect of int , long int or long long int on time

Out of curiosity, I want to ask what difference does it make to use int , long int or long long int on the time taken by the code to execute successfully. I already know that it can cause Wrong Answer but how does it affect on time?

asked 20 Mar '17, 13:59

monalshadi's gravatar image

2★monalshadi
524
accept rate: 16%


This question has been asked several times. I will include the relevant links soon (give me ~10-15 min). EDIT- Here is the link Why long long int causes TLE

The basic concept is that, a long long int will take two operations instead of 1 on it.

Visualisation- A 32 bit system, which has 32-bit cells to store stuff, would store 64-bit long long int by "splitting" it into two 32 bit ones and storing each in a cell and working on both of them. So number of operations double.

Basic idea is that long long int take twice the operation than int. Eg, if (int) 5+3 is 1 operation, but (long long int ) 5+3 is 2 operation

link

answered 20 Mar '17, 14:27

vijju123's gravatar image

5★vijju123 ♦♦
15.4k12066
accept rate: 18%

edited 20 Mar '17, 14:32

That sound's more convincing that how using long long int instead of long int can cause tle. Thanks!

(20 Mar '17, 19:47) monalshadi2★

Check it for yourself. Run these codes on the Code, Compile and Run and check the time taken below:

int i,j; for(i=0;i<1e+9;i++) j=sqrt(i);


long long i,j; for(i=0;i<1e+9;i++) j=sqrt(i);


I hope you will understand the difference ! .

link

answered 20 Mar '17, 14:32

pankajkhan's gravatar image

5★pankajkhan
1.2k10
accept rate: 15%

That's a very elegant, practical example.

(20 Mar '17, 14:34) vijju123 ♦♦5★
1

Thank for help @pankajkhan :)

(20 Mar '17, 19:46) monalshadi2★
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:

×720
×204
×7

question asked: 20 Mar '17, 13:59

question was seen: 291 times

last updated: 20 Mar '17, 19:47