1

if I have a long int and long and i have to assign the same value to both of them which of them will get read fast

#inlude<stdio.h>
int main(){
    long long int  i;
    long int j;
    int k;
    scanf("%lld %ld %d",&i,&j,&k);
    return 0;
}

My doubt is in which of them the data will get stored first ????

Joseph Young
  • 2,758
  • 12
  • 23
  • do you really care microseconds of difference? – user3528438 Feb 29 '16 at 22:02
  • 1
    @user3528438 It's nano or even pico... – Eugene Sh. Feb 29 '16 at 22:04
  • he green variable is always faster than the red or the blue one - Seriously: what is your problem? How fast can you type? – too honest for this site Feb 29 '16 at 22:06
  • i am talking about 0.01 seconds – user5910213 Feb 29 '16 at 22:16
  • 1
    It won't make a *measurable* difference. First of all, in this specific example, the `scanf` call will take as long as necessary to scan all three items, so its performance will be governed by the slowest of the three; you'd want to split this into three different `scanf` calls to see if there was a difference at all. Even if there was, it would be down in the noise relative to the time it takes to perform the context switch. – John Bode Feb 29 '16 at 22:17
  • @user5910213: I can pretty much guarantee that the difference between scanning a `long` vs. an `int` will be at least a couple of orders of magnitude smaller than 0.01 seconds. Not to mention that the bulk of that time is spent parsing the input string as opposed to storing the result. – John Bode Feb 29 '16 at 22:19
  • It might be architecture dependent, so please feel free to correct me. But once scanf scanned all three items, and compiler can see that none of those loads affect each other, wont it pipeline all of them and store them in one cycle? Considering this was running on modern processor. I guess this assumption is too broad – SandBag_1996 Feb 29 '16 at 22:58
  • @UnderDog Note, "can see that none of those loads affect each other" is not important as the pointers passed to `scanf(...)` are supposed to be of non-overlapping memory. – chux - Reinstate Monica Feb 29 '16 at 23:16
  • @chux, thank you for replying. I appreciate it. Can you explain a bit more why they are supposed to be of non-overlapping memory? Intuitively, I would think otherwise, because they are independent memory accesses. – SandBag_1996 Feb 29 '16 at 23:19
  • Your title refers to `long int` and `int`. The first sentence of your question refers to `long int` and `long` (which are two names for the same type). Your code uses `long long int`, `long int`, and `int`. Please update your question to clarify this. While you're at it, please copy-and-paste your actual code; the code in the question has a typo on the first line that will prevent it from compiling. Finally, are you asking about the *speed* or the *order* of the read operations (or both)? – Keith Thompson Mar 01 '16 at 00:41

2 Answers2

1

The performance of scanf() of int and long is certainly of insignificant difference. But OP asks another, more interesting question

My doubt is in which of them the data will get stored first ????

C does specify "The fscanf function executes each directive of the format in turn." which strongly suggest i is written before j, etc. I take that to mean the processing of text input happens in the order of of the format, but is just short of specifying the order in which i,j,k are written.

I have my doubts about the following and have posted a question.
To be clear, if i,j,k are all scanned, the library may write k first.

How is code to tell and why should it care? The order in which the incoming text and how it is applied to i,j,k is specified. Of course, if only 1 or 2 of the 3 are scanned. k is not written.

Best to check the return value as that informs the calling code the success of the call.

switch (scanf("%lld %ld %d",&i,&j,&k)) {
  case 3: puts("Success"); break;
  case 2: puts("Only i,j"); break;
  case 1: puts("Only i"); break;
  case 0: puts("None"); break;
  case EOF: puts("EOF or input error"); break;
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

Since ints are usually smaller than long longs and longs, it would make sense the int will read in faster, but not enough to notice a difference in computing I think.

int vs long vs long long

int faster than long

Community
  • 1
  • 1
Abner
  • 9
  • 5