Thread: Reading Huge input from the terminal in least time.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Reading Huge input from the terminal in least time.

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Typing a million of anything is going to take too much time.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    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;
    }
    Perhaps you should show us what you actually did, rather than just complain "it's too slow" without backing it up with some evidence.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input from terminal
    By kiros88 in forum C Programming
    Replies: 1
    Last Post: 09-17-2009, 01:12 PM
  2. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  3. Reading terminal arguments
    By DenKain in forum C++ Programming
    Replies: 24
    Last Post: 10-07-2008, 04:37 PM
  4. Writing to/reading from terminal window
    By ac251404 in forum Windows Programming
    Replies: 0
    Last Post: 05-19-2006, 01:06 PM
  5. Getting user input without echo to terminal
    By cliff78 in forum C Programming
    Replies: 1
    Last Post: 04-27-2004, 08:03 PM