Re: *scanf efficiency for patterns

Discussion in 'C Programming' started by Malcolm, Aug 27, 2003.

  1. Malcolm

    Malcolm Guest

    "Christopher Benson-Manica" <> wrote in message
    >
    > How efficient are the *scanf functions when used for pattern matching?
    > Specifically, is something like
    >

    The real answer is "probably efficient enough for it not to matter".
    Typically a program will only process a relatively small quantity of
    strings, and processing time will be dwarfed by time taken to input the
    string.

    scanf() is a general-purpose routine, so it is probably possible to beat it
    by coding a dedicated function. However since scanf() is in the standard
    library it may well be written in native machine code and heavily optimised,
    an investment which would be expensive for a one-off, unless of course it is
    absolutely vital to squeeze the last drop of performance in that particular
    function.
    Malcolm, Aug 27, 2003
    #1
    1. Advertising

  2. Malcolm

    Eric Sosman Guest

    Malcolm wrote:
    >
    > "Christopher Benson-Manica" <> wrote in message
    > >
    > > How efficient are the *scanf functions when used for pattern matching?
    > > Specifically, is something like
    > >

    > The real answer is "probably efficient enough for it not to matter".
    > Typically a program will only process a relatively small quantity of
    > strings, and processing time will be dwarfed by time taken to input the
    > string.
    >
    > scanf() is a general-purpose routine, so it is probably possible to beat it
    > by coding a dedicated function. However since scanf() is in the standard
    > library it may well be written in native machine code and heavily optimised,
    > an investment which would be expensive for a one-off, unless of course it is
    > absolutely vital to squeeze the last drop of performance in that particular
    > function.


    An anecdote for the "You never can tell" file:

    One C implementation I ran into some years ago did just
    as Malcolm said: It used hand-crafted assembly language for
    many of the library functions. The result? Programs ran
    *slower* than with naive pure-C implementations!

    The architecture featured a fairly rich set of string-
    bashing instructions, and the library implementors used them:
    after all, loading a few registers and executing one machine
    instruction must be faster than an ordinary loop, right?
    Alas, the particular machine at hand was based on a cost-
    reduced design that didn't implement the entire instruction
    set in hardware, but instead provided trap-and-emulate code
    for the missing instructions ... I achieved an *eightfold*
    speedup in our PostScript generation by substituting plain-C
    implementations of *printf() for the "optimized" versions.

    Moral: Measure, measure, measure!

    --
    Eric Sosman, Aug 27, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Krone

    perl efficiency -- fastest grepping?

    Bryan Krone, Nov 5, 2004, in forum: Perl
    Replies:
    1
    Views:
    1,465
    Jim Gibson
    Nov 8, 2004
  2. crichmon
    Replies:
    4
    Views:
    471
    Mabden
    Jul 7, 2004
  3. Peter Nilsson

    Re: *scanf efficiency for patterns

    Peter Nilsson, Aug 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    376
    Peter Nilsson
    Aug 27, 2003
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,346
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    660
    Richard Bos
    May 2, 2006
Loading...

Share This Page