Hi there,
Suppose we have a 2-d array arr[1000][1000] and input is supplied one row at a time, Then what is the best possible way to read this much data in the least time as scanf() take too much time in reading from the keyboard.
Hi there,
Suppose we have a 2-d array arr[1000][1000] and input is supplied one row at a time, Then what is the best possible way to read this much data in the least time as scanf() take too much time in reading from the keyboard.
Typing a million of anything is going to take too much time.
Quzah.
Hope is the first step on the road to disappointment.
What do you mean "too much time".
Sure, it'll take a lot of time if you're typing them in.
But to read from a file, using redirection, takes almost no time at all.
Perhaps you should show us what you actually did, rather than just complain "it's too slow" without backing it up with some evidence.Code:Generate 1M numbers to test with $ perl -e 'for($i=0;$i<1000000;$i++){print int(rand()*1000) . "\n";};' > 1m.txt Less than 0.5 seconds, on this <2GHz laptop $ time ./a.out < 1m.txt real 0m0.362s user 0m0.356s sys 0m0.008s The simple test code $ cat bar.c #include <stdio.h> int main ( ) { int foo; while ( scanf("%d",&foo) == 1 ) { } return 0; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.