473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Good function to test speed of language?

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 =)

Nov 14 '05 #1
6 1956
<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).

tr*****@gmail.com 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
Nov 14 '05 #2
In article <11*********************@o13g2000cwo.googlegroups. com>,
<tr*****@gmail.com> 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[i] = Y[i] * 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!
Nov 14 '05 #3
On 30 Mar 2005 09:54:11 -0800, tr*****@gmail.com
<tr*****@gmail.com> 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
Nov 14 '05 #4
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.

Nov 14 '05 #5
Chris Croughton wrote:
On 30 Mar 2005 09:54:11 -0800, tr*****@gmail.com
<tr*****@gmail.com> 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

Nov 14 '05 #6
On 30 Mar 2005 18:26:40 -0800, be*******@aol.com 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
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
10
by: Ken VdB | last post by:
Hi everyone, Is there a reason why the Mid() function only works in one direction in VBScript? This code works in VB6 but not in VBScript? Is there a way around it? I am trying to create an...
39
by: Suresh | last post by:
Hi, I am new to C and curious to know some basic questions about C. Why C is good? In what way its better than any other languages? If it is no longer a choice of programming language...why...
8
by: markus | last post by:
Hi, As I been programming in C I have discovered more and more tools that really helps to speed up the development process when writing programs. Examples of such tools are cscope, cbrowser,...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
6
by: Dasn | last post by:
Hi, there. 'lines' is a large list of strings each of which is seperated by '\t' I wanna split each string into a list. For speed, using map() instead of 'for' loop. 'map(str.split, lines)'...
15
by: Chris | last post by:
This is just some dummy code to mimic what's being done in the real code. The actual code is python which is used as a scripting language in a third party app. The data structure returned by the...
4
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.