Good function to test speed of language?

Discussion in 'C Programming' started by trentdk@gmail.com, Mar 30, 2005.

  1. Guest

    I want to test which language (testing C and FORTRAN) would be faster
    with math calculations; one test with intergers, and another test with
    floats. What math formulas/functions would you guys use that are
    simple, yet will tax the processor and the abilities of the languages?

    Thanks =)
     
    , Mar 30, 2005
    #1
    1. Advertising

  2. <posted & mailed>

    This is the wrong question. Performance is not likely to language-specific,
    but rather determined by the compiler's quality at optimizing code for the
    target CPU. If there is an optimal solution, and your compiler's are
    intelligent enough to settle on that one solution, they will generate the
    same machine code regardless of language (in practice, that doesn't happen
    because compilers can never be made that intelligent).


    You want to code up a representative example of what you are going to do and
    test it with each.

    Also, be sure to use the same precision in both situations (FORTRAN defaults
    to single-precision transcendental functions, whereas C uses
    double-precision ones; you can obtain single precision libraries for C as
    well).

    wrote:

    > I want to test which language (testing C and FORTRAN) would be faster
    > with math calculations; one test with intergers, and another test with
    > floats. What math formulas/functions would you guys use that are
    > simple, yet will tax the processor and the abilities of the languages?
    >
    > Thanks =)


    --
    Remove '.nospam' from e-mail address to reply by e-mail
     
    James McIninch, Mar 30, 2005
    #2
    1. Advertising

  3. In article <>,
    <> wrote:
    :I want to test which language (testing C and FORTRAN) would be faster
    :with math calculations; one test with intergers, and another test with
    :floats. What math formulas/functions would you guys use that are
    :simple, yet will tax the processor and the abilities of the languages?

    There is an art to writing useful benchmarks, and it is an art that
    not many can do well.

    It is fairly common now for C and Fortran to have distinct
    parsers, but that after the top-level analysis has been done,
    for everything else about the compilers to be shared. On
    such systems, the optimization facilities are pretty much
    identical between the languages, so often neither will have
    a clear execution time advantage over the other for the same
    kind of work.

    *Historically*, Fortran was faster for mathematics. This
    was, to a fair degree, due to the fact that Fortran had no
    pointers, and so opportunities for optimization were clearer
    in Fortran code, as one did not have to worry about
    "pointer aliasing".


    The rule of thumb is that the more straight-forward
    you are in writing your code (C or Fortran), the easier
    it is for the optimization phases to act. For example,
    a Fortran loop such as

    DO 10, I = 5, 30
    10 X(I) = Y(I)*3.141 + 2.71728

    might be optimized better than the C code

    for( xp = X+4, yp = Y+4, i = 5; i < 30; i++ )
    *xp++ = *yp++ * 3.141 + 2.71728

    This would not be because C was "less efficient" than
    Fortran, but just because C had to worry about pointer
    overlap and possibly retaining final pointer and index
    values, and so on. If the same code were rewritten
    in a more Fortran style in C, with an explicit local
    variable,

    { int i;
    for ( i = 4; i < 30; i++ )
    X = Y * 3.141 + 2.71728;
    }

    then the C compiler would [these days] probably compile
    it equally well as the Fortran compiler would.



    In -some- algorithms, with unusual memory accesses, C can
    do better because the algorithm writer can use pointers in ways
    that are too complex to be jury-rigged by the Fortran compiler.
    But such cases are not as common as one might expect: modern
    compilers are pretty good with pointer <-> index equivilences
    for both languages.


    There simply isn't any one benchmark that can really test
    whether C or Fortran is "better" for mathematics. A great deal
    depends upon picyune details of the underlying architecture,
    such as the number of bytes on a cache line, the size of the
    primary and secondary caches, the extent to which registers
    are "general purpose" or dedicated to addresses or integers
    or floating point numbers, what addressing modes are
    available, and details of instruction scheduling and
    jump slots. Sometimes the most efficient code looks very
    different than how one would normally think to code the problem --
    for example, the most efficient code might involve loop unrolling,
    working in blocks of memory [so as to avoid cache transitions],
    and might involve starting computations earlier than would seem
    "natural", so that the result of the computation "graduates"
    through the processor pipeline at just the right point.

    The best thing you can do is code up a representative program
    in both languages, and use the compiler and any code analysis
    tools available to optimize the heck out of both of them. But
    unless you have a problem that is going to take a -lot- of
    computer time (or the computer time is very expensive) then it
    often turns out that the human time (and salary costs) of
    doing this kind of detailed optimization are much much higher
    than the total time one would eventually save over all
    executions over the program lifetime. Optimize in cases
    that are proven to be problems, rather than spending a long
    time on optimizations that might end up saving half a minute
    over 3 years.

    Note too that you might have the option of combining
    the two languages together: your operating system might define
    operating-system specific ways of linking C and Fortran together.
    You could then write code in the most appropriate language for
    the problem at hand rather than worrying much about optimization
    tradeoffs by chosing one or the other language.
    --
    Oh, to be a Blobel!
     
    Walter Roberson, Mar 30, 2005
    #3
  4. On 30 Mar 2005 09:54:11 -0800,
    <> wrote:

    > I want to test which language (testing C and FORTRAN) would be faster
    > with math calculations; one test with intergers, and another test with
    > floats. What math formulas/functions would you guys use that are
    > simple, yet will tax the processor and the abilities of the languages?


    It's not possible. All you will test is which compiler produces the
    fastest code on a particular system with particular options (for
    instance, I would expect gcc and g77 to produce equally good code with
    the same optimisation level and reasonably written input -- I've done
    that test for C and C++ using gcc and g++ and the only difference was
    the compilation time).

    The only thing likely to be significantly different is array access
    using subscripts (especially multi-dimensional arrays). However, since
    C has pointers and Fortran doesn't, a program written to use pointers
    instead of array subscripts may well be just as fast (or even faster).

    Fortran has the advantage of having man of special purpose numerical
    libraries available, many of which have not been translated into C so
    the "C versions" are wrappers round the Fortran ones and can be
    inefficient due to the different array conventions. It also has a built
    in 'complex' type, which many C compilers don't have yet (or at least
    don't have in the standard form).

    But as far as straight arithmetic operations are concerned, I would be
    very surprised to see any consistent difference between the languages.

    (I haven't written any Fortran for almost 15 years, I'd have to find a
    manual to write even "Hello world", the language has changed a lot since
    FORTRAN IV...)

    Chris C
     
    Chris Croughton, Mar 30, 2005
    #4
  5. Guest

    James McIninch wrote:

    <snip>

    > Also, be sure to use the same precision in both situations (FORTRAN

    defaults
    > to single-precision transcendental functions, whereas C uses
    > double-precision ones; you can obtain single precision libraries for

    C as
    > well).


    The intrinsics of Fortran, such as COS, have been generic since at
    least the Fortran 77 standard, meaning that COS(X) will call a single
    or double precision version of the cosine function, and return a single
    or double precision real, depending on the type of variable X.
     
    , Mar 31, 2005
    #5
  6. Richard Bos Guest

    -cnrc.gc.ca (Walter Roberson) wrote:

    > The rule of thumb is that the more straight-forward
    > you are in writing your code (C or Fortran), the easier
    > it is for the optimization phases to act. For example,
    > a Fortran loop such as
    >
    > DO 10, I = 5, 30
    > 10 X(I) = Y(I)*3.141 + 2.71728
    >
    > might be optimized better than the C code
    >
    > for( xp = X+4, yp = Y+4, i = 5; i < 30; i++ )
    > *xp++ = *yp++ * 3.141 + 2.71728
    >
    > This would not be because C was "less efficient" than
    > Fortran, but just because C had to worry about pointer
    > overlap and possibly retaining final pointer and index
    > values, and so on.


    If you're lucky enough to have a good C99 compiler, you can get a
    similar effect in C using restrict.

    Richard
     
    Richard Bos, Mar 31, 2005
    #6
  7. invincible Guest

    speed of ur application depends upon
    1. compiler
    2. architecture of system

    compiler does lot of optimization before generating asm instruction.
    so u should check out optimised asm instruction got from different lang
    compilers.

    secondly it depends on processing of instruction , generally it is believed
    that intel processors are very aggressive. u dont have control over it.

    Mohan
    <> wrote in message
    news:...
    > I want to test which language (testing C and FORTRAN) would be faster
    > with math calculations; one test with intergers, and another test with
    > floats. What math formulas/functions would you guys use that are
    > simple, yet will tax the processor and the abilities of the languages?
    >
    > Thanks =)
    >
     
    invincible, Mar 31, 2005
    #7
  8. Guest

    Chris Croughton wrote:
    > On 30 Mar 2005 09:54:11 -0800,
    > <> wrote:
    >
    > > I want to test which language (testing C and FORTRAN) would be

    faster
    > > with math calculations; one test with intergers, and another test

    with
    > > floats. What math formulas/functions would you guys use that are
    > > simple, yet will tax the processor and the abilities of the

    languages?
    >
    > It's not possible. All you will test is which compiler produces the
    > fastest code on a particular system with particular options (for
    > instance, I would expect gcc and g77 to produce equally good code

    with
    > the same optimisation level and reasonably written input -- I've done
    > that test for C and C++ using gcc and g++ and the only difference was
    > the compilation time).
    >
    > The only thing likely to be significantly different is array access
    > using subscripts (especially multi-dimensional arrays). However,

    since
    > C has pointers and Fortran doesn't, a program written to use pointers
    > instead of array subscripts may well be just as fast (or even

    faster).
    >
    > Fortran has the advantage of having man of special purpose numerical
    > libraries available, many of which have not been translated into C so
    > the "C versions" are wrappers round the Fortran ones and can be
    > inefficient due to the different array conventions. It also has a

    built
    > in 'complex' type, which many C compilers don't have yet (or at least
    > don't have in the standard form).
    >
    > But as far as straight arithmetic operations are concerned, I would

    be
    > very surprised to see any consistent difference between the

    languages.
    >
    > (I haven't written any Fortran for almost 15 years, I'd have to find

    a
    > manual to write even "Hello world", the language has changed a lot

    since
    > FORTRAN IV...)


    Even Fortran 2003 is largely (but not entirely) backwards compatible
    with Fortran IV (later standardized as Fortran 66), and some current
    Fortran compilers (implementing Fortran 95) include options for Fortran
    IV compatibility, such as /f66 in Compaq Visual Fortran.

    The code

    WRITE (6,'('' Hello World!'')')
    END

    is a valid "Hello World" program in F66, F77, F90, F95, and F2003, and
    almost any F77 or later compiler will accept the cleaner

    WRITE (*,*) 'Hello World'
    END
     
    , Mar 31, 2005
    #8
  9. On 30 Mar 2005 18:26:40 -0800, wrote:

    > James McIninch wrote:
    >
    > <snip>
    >
    > > Also, be sure to use the same precision in both situations (FORTRAN

    > defaults
    > > to single-precision transcendental functions, whereas C uses
    > > double-precision ones; you can obtain single precision libraries for

    > C as
    > > well).

    >
    > The intrinsics of Fortran, such as COS, have been generic since at
    > least the Fortran 77 standard, meaning that COS(X) will call a single
    > or double precision version of the cosine function, and return a single
    > or double precision real, depending on the type of variable X.


    variable _or value_ X. Explicitly declared variables can be equally
    easily single or double -- or other system-dependent non-portable
    precisions. Implicitly declared variables, such as under the hoary
    "God is real" rule, are more easily single. Fortran floating-point
    literals are single unless you use 'd' for the exponent, or in >= F90
    the _kind syntax. Double real must occupy twice the space of single,
    but need not actually be twice as precise.

    C99, not yet widely implemented/available, includes single=float and
    long double math functions as well as the classic double ones, and
    adds complex variants where applicable, plus optional (if you #include
    <tgmath.h>) generic 'wrappers' comparable to Fortran (and others).
    (Since C89) long double although a distinct type need not actually be
    more precise or bigger than double, especially in the M$ world; for
    that matter double need not be better than float if that satisfies the
    (minimum) requirements. Floating literals in C are double unless you
    append 'f' float or 'l' long double.

    Both languages permit calculations to be performed in greater than the
    standardly-specified precision (and range) if the compiler prefers,
    especially if as on a certain common machine it is costly to convert.

    The OP also asked about integers. Fortran doesn't standardly require
    more than one size/precision of integer, nor any unsigned. C has four
    and C99 five nominally distinct precisions, in both signed and
    unsigned (at least <G>), although again the 'higher' ones need not
    actually be more precise (or bigger) as long as they meet minima.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Apr 9, 2005
    #9
    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. Ham

    I need speed Mr .Net....speed

    Ham, Oct 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    2,347
    Antony Baula
    Oct 29, 2004
  2. efiedler
    Replies:
    1
    Views:
    2,054
    Tim Ward
    Oct 9, 2003
  3. Replies:
    2
    Views:
    2,298
    Howard
    Apr 28, 2004
  4. Replies:
    2
    Views:
    335
    Christopher Benson-Manica
    Apr 28, 2004
  5. Casey Hawthorne
    Replies:
    18
    Views:
    631
    Beej Jorgensen
    Nov 6, 2009
Loading...

Share This Page