Which is faster? - Programming Languages

This is a discussion on Which is faster? - Programming Languages ; Hi, I am figuring when I want to read a line (maybe 2M bytes long) from a file. Which method is faster in C++? 1. for(; getchar() 2. getline(file, string) Thanks! Regards! Bo...

+ Reply to Thread
Results 1 to 10 of 10

Which is faster?

  1. Default Which is faster?

    Hi,
    I am figuring when I want to read a line (maybe 2M bytes long) from
    a file. Which method is faster in C++?

    1. for(; getchar()
    2. getline(file, string)

    Thanks!

    Regards!
    Bo

  2. Default Re: Which is faster?

    On 10 Nov, 10:28, Bo Yang <struggl...@gmail.com> wrote:
    [color=blue]
    >    I am figuring when I want to read a line (maybe 2M bytes long) from
    > a file. Which method is faster in C++?
    >
    > 1. for(; getchar()
    > 2. getline(file, string)[/color]

    well you could just time 'em. I'd bet on 2 if I were a betting man
    because it involves less function calls.

    Note: as written 1 never terminates...

    You might consider fread().


    --
    Nick Keighley


  3. Default Re: Which is faster?

    Bo Yang wrote:[color=blue]
    > Hi,
    > I am figuring when I want to read a line (maybe 2M bytes long) from
    > a file. Which method is faster in C++?
    >
    > 1. for(; getchar()
    > 2. getline(file, string)[/color]

    Why not try it out? On as many different OSes, files, and hardware as
    you can find.

    Looking at it logically, it seems to me #2 should be faster, as
    'getline' implementations usually are aware of read buffering. If you
    want to write your own version and it should be fast, just read 3 Mb of
    data in one huge block (usually --again-- that's the fastest way to read
    any data) and find the line end in there.

    [Jw]

  4. Default Re: Which is faster?

    Nick Keighley wrote:[color=blue]
    > On 10 Nov, 10:28, Bo Yang <struggl...@gmail.com> wrote:
    >[color=green]
    >> I am figuring when I want to read a line (maybe 2M bytes long) from
    >> a file. Which method is faster in C++?
    >>
    >> 1. for(; getchar()
    >> 2. getline(file, string)[/color]
    >
    > well you could just time 'em. I'd bet on 2 if I were a betting man
    > because it involves less function calls.[/color]

    On my machine, 2 involves more function calls.
    The C implementation on my machine,
    implements getchar as a macro.

    --
    pete

  5. Default Re: Which is faster?

    On Nov 10, 7:56 pm, "[Jongware]" <so...@no.spam.net> wrote:[color=blue]
    > Bo Yang wrote:[color=green]
    > > Hi,
    > >    I am figuring when I want to read a line (maybe 2M bytes long) from
    > > a file. Which method is faster in C++?[/color]
    >[color=green]
    > > 1. for(; getchar()
    > > 2. getline(file, string)[/color]
    >
    > Why not try it out? On as many different OSes, files, and hardware as
    > you can find.
    >
    > Looking at it logically, it seems to me #2 should be faster, as
    > 'getline' implementations usually are aware of read buffering. If you
    > want to write your own version and it should be fast, just read 3 Mb of
    > data in one huge block (usually --again-- that's the fastest way to read
    > any data) and find the line end in there.[/color]

    Yeah, thank you for all your replies.

    Regards!
    Bo


  6. Default Re: Which is faster?

    Nick Keighley said:
    [color=blue]
    > On 10 Nov, 10:28, Bo Yang <struggl...@gmail.com> wrote:
    >[color=green]
    >> I am figuring when I want to read a line (maybe 2M bytes long) from
    >> a file. Which method is faster in C++?
    >>
    >> 1. for(; getchar()
    >> 2. getline(file, string)[/color]
    >
    > well you could just time 'em. I'd bet on 2 if I were a betting man
    > because it involves less function calls.[/color]

    Fewer, man! fewer! :-)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

  7. Default Re: Which is faster?

    Bo Yang wrote:[color=blue]
    >
    > I am figuring when I want to read a line (maybe 2M bytes long)
    > from a file. Which method is faster in C++?
    >
    > 1. for(; getchar()
    > 2. getline(file, string)[/color]

    A 2 Megabyte line seems excessive, and I greatly doubt you will be
    able to reserve a suitable buffer outside the malloc system. I
    suggest you rethink the application.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.

  8. Default Re: Which is faster?

    On Nov 11, 7:19 am, CBFalconer <cbfalco...@yahoo.com> wrote:[color=blue]
    > Bo Yang wrote:
    >[color=green]
    > > I am figuring when I want to read a line (maybe 2M bytes long)
    > > from a file. Which method is faster in C++?[/color]
    >[color=green]
    > > 1. for(; getchar()
    > > 2. getline(file, string)[/color]
    >
    > A 2 Megabyte line seems excessive, and I greatly doubt you will be
    > able to reserve a suitable buffer outside the malloc system.  I
    > suggest you rethink the application.[/color]

    And for another words, how does getline implemented inside the
    library? Does it allocate some buffer and read the file and then
    detect a '\n' in the buffer?

    Thanks!

    Regards!
    Bo

  9. Default ' implementations usually

    it seems to me #2 should be faster, as
    > 'getline' implementations usually are aware of read buffering. If you
    > want to write your own version and it should be fast, just read 3 Mb of
    > data in one huge block (usually --again-- that's the fastest way to read
    > any data) and find the line end in there.

  10. Default Re: Which is faster?

    To get your doctorate in psychology is generally 8 years, same as to become an OB-GYN

+ Reply to Thread