Performance Programming

  • Thread starter Andrew Au \(Newsgroup\)
  • Start date
A

Andrew Au \(Newsgroup\)

Hi all,

I am switching from Java to C solely for performance, but I wonder are
there any coding techniques that can boost performance in C? I am asking
such an open-ended question to elicit response for me such as links and
discussion.

By the way, I am also pretty headache with ArrayIndexOutOfBound
exception, in which C or C++ never warn about anything reading uninitialized
memory, can I make a trap at all memory dereferencing point, so that I can
check if the piece of memory is valid, such as array indexing, or picking
member? Anyway this can be done except instrumenting code by tools such as
Purify?

Andrew
 
C

Christian Bau

"Andrew Au \(Newsgroup\) said:
Hi all,

I am switching from Java to C solely for performance, but I wonder are
there any coding techniques that can boost performance in C? I am asking
such an open-ended question to elicit response for me such as links and
discussion.

That's really clever. You switch from Java to C to make programs faster,
but you have no idea how to make them faster? How about asking in
comp.lang.java for techniques that boost performance in Java?
 
A

Andrew Au \(Newsgroup\)

Because C is more close the machine language, I could possibly increase the
performance by exploiting machine dependent characteristics, for example,
using SSE instruction, ...

I am not sure about that, therefore I asked.
 
N

Nicolas Pavlidis

Andrew said:
Because C is more close the machine language, I could possibly increase the
performance by exploiting machine dependent characteristics, for example,
using SSE instruction, ...

In your case I'd learn the Basics in C and then switch to C++. Or even
do only C++ but with the all "primitive" ... er imperativ ( :) )
languageproperties C++ has, so you can write clean OO - code with some
speed up mechanism.
But the best measure to speed um code is to let it be done by the
compiler of your choice.

Kind regards,
Nicolas

P.S. But note, C++ is like C maschine - dependent!
 
D

Derrick Coetzee

Andrew said:
I am switching from Java to C solely for performance, but I wonder are
there any coding techniques that can boost performance in C? I am asking
such an open-ended question to elicit response for me such as links and
discussion.

It's been said before, but I'll say it again. When it comes to
optimization, follow the 3 golden rules:

1. Write all your code in the cleanest possible manner first.
2. Profile to find hot spots.
3. Attempt to optimize hot spots, always measuring to see how much
improvement you get. Revert ineffective optimizations.

Unless you're writing an operating system, graphics engine, compiler,
numerical library, or some other complex computationally intensive
application, you'll probably be fine with Java if you just follow this
approach. The same approach applies to C and C++ - you don't try to
boost performance up front, but once you've found the critical sections
there are many tricks you can try, although many have been made obsolete
by modern compiler optimizations.
 
K

Kieran Simkin

Andrew Au (Newsgroup) said:
Because C is more close the machine language, I could possibly increase
the
performance by exploiting machine dependent characteristics, for example,
using SSE instruction, ...

Please don't top-post.

A lot of people seem to say C is closer to "the machine", this fact is
debatable. One thing is true, you cannot explicitly make use of SSE
instructions (or MMX or any others) with pure C. An optimizing compiler may
make use of them on your behalf, but I wouldn't depend on it. If you really
want to take advantage of SSE, MMX etc, you will have to start writing ASM.
By the way, I am also pretty headache with ArrayIndexOutOfBound
exception, in which C or C++ never warn about anything reading
uninitialized
memory, can I make a trap at all memory dereferencing point, so that I can
check if the piece of memory is valid, such as array indexing, or picking
member? Anyway this can be done except instrumenting code by tools such as
Purify?

Part of the reason C is considered by many to be faster than languages such
as Java is precisely because it doesn't do run-time checking of array bounds
and suchlike automatically on your behalf. There are libraries and compiler
hacks that will give you this functionality, but if you're switching to C
for speed, you'd be better off just writing good code (ie, checking array
bounds yourself where necessary).

As another poster mentioned, learn C first, then worry about optimising it.


~Kieran Simkin
Digital Crocus
http://digital-crocus.com/
 
C

Christian Bau

"Andrew Au \(Newsgroup\) said:
Because C is more close the machine language, I could possibly increase the
performance by exploiting machine dependent characteristics, for example,
using SSE instruction, ...

Have a look at comp.asm.x86 or whatever it is called. comp.lang.c is the
wrong newgroup.
 
A

Andrew Au \(Newsgroup\)

Kieran Simkin said:
Please don't top-post.

Sorry for the top post, it is a tradition in Hong Kong newsgroups to
top-post, post at the bottom will be cursed :p
BTW, while using machine dependent characteristic may not help, there maybe
some idea that I have no idea, that's
why I posted the message so open-ended, I don't want the discussion end up
discussing only one thing.
A lot of people seem to say C is closer to "the machine", this fact is
debatable. One thing is true, you cannot explicitly make use of SSE
instructions (or MMX or any others) with pure C. An optimizing compiler may
make use of them on your behalf, but I wouldn't depend on it. If you really
want to take advantage of SSE, MMX etc, you will have to start writing ASM.

Part of the reason C is considered by many to be faster than languages such
as Java is precisely because it doesn't do run-time checking of array bounds
and suchlike automatically on your behalf. There are libraries and compiler
hacks that will give you this functionality, but if you're switching to C
for speed, you'd be better off just writing good code (ie, checking array
bounds yourself where necessary).

Array Bound Checking could be switched off when I am making a release, but
it is essential for debugging.
How many hours you guys have been using to trace bug about off-by-one error?
What can you suggest if that is really necessary?

I have many lines in my code like these:

trainingContext->posterior.gaussian_posterior_probabilities[t][trainingConte
xt->gaussian_sequence[q][j-1][m]] =

trainingContext->posterior.state_posterior_probabilities[t][trainingContext-
state_sequence[q][j]] + ....

Although I managed my own MemoryPool, but you know I won't check the pointer
for it validity before I try to dereference it.
I really need a way for the language itself (or libraries, or compiler hack,
or whatever) to help me the check if the pointer is valid, allocated before
I dereference it and never give me ANY uninitialized values. In such cases,
I prefer an exception and quit the program immediately.

What is really need is something like these:

void validityCheck(void* valid) {
if (!isValid()) {
printf("Accessing uninitialized memory\n");
// I knew it is almost impossible in C to get me a stack trace here, it
would be even better if a stack trace is available.
exit(255);
}
}

#ifndef NDEBUG
int main(int argc, char** argv) {
register_dereferencing_listener(validityCheck);
// My business logic
}
#endif
 
P

Papadopoulos Giannis

Andrew said:
Hi all,

I am switching from Java to C solely for performance, but I wonder are
there any coding techniques that can boost performance in C? I am asking
such an open-ended question to elicit response for me such as links and
discussion.

By the way, I am also pretty headache with ArrayIndexOutOfBound
exception, in which C or C++ never warn about anything reading uninitialized
memory, can I make a trap at all memory dereferencing point, so that I can
check if the piece of memory is valid, such as array indexing, or picking
member? Anyway this can be done except instrumenting code by tools such as
Purify?

Andrew
Ever heard of JNI (Java Native Interface)?
Check this first
 
R

Randy Howard

"Andrew Au \(Newsgroup\)" said:
Hi all,

I am switching from Java to C solely for performance, but I wonder are
there any coding techniques that can boost performance in C? I am asking
such an open-ended question to elicit response for me such as links and
discussion.

Surest method is to wait six months before you start development, then buy
the latest/greatest processor speed available at that time. That's likely
to make far more difference in the general case than any specific programming
tactic.

It's probable that if you posted this sort of question in comp.programming,
but with some actual specifics as to what code you are writing, and why you
think you are having performance problems, you might get some good
responses.
 
A

ala

Please don't top-post.

A lot of people seem to say C is closer to "the machine", this fact is
debatable. One thing is true, you cannot explicitly make use of SSE
instructions (or MMX or any others) with pure C. An optimizing compiler may
make use of them on your behalf, but I wouldn't depend on it. If you really
want to take advantage of SSE, MMX etc, you will have to start writing ASM.


Part of the reason C is considered by many to be faster than languages such
as Java is precisely because it doesn't do run-time checking of array bounds
and suchlike automatically on your behalf. There are libraries and compiler
hacks that will give you this functionality, but if you're switching to C
for speed, you'd be better off just writing good code (ie, checking array
bounds yourself where necessary).

if "yourself" is a programm that controll (in debug mode) if there is
something out "bounds" I'm agree elsewere not
As another poster mentioned, learn C first, then worry about optimising it.

Why?
 
F

Flash Gordon

On Sat, 11 Sep 2004 16:46:45 GMT, "Kieran Simkin"


if "yourself" is a programm that controll (in debug mode) if there is
something out "bounds" I'm agree elsewere not

C does not have a "debug mode". Some implementations provide such
things, and some might detect out of bounds accesses, but this is
nothing to do with the C *language*. It is also highly unlikely that
most C implementations will provide complete bounds checking the was
some other languages are.

Because if you don't know the language you don't stand a chance of
writing a program to do what you want, let alone writing one that does
what you want and is fast enough. If you don't know the language then
you might as well use this program:

int main(void)
{
return 0;
}

stands more of a chance of meeting one of the requirements (speed) than
a program written by someone who has not learnt the language.
 
K

Kelsey Bjarnason

[snips]

Because C is more close the machine language, I could possibly increase the
performance by exploiting machine dependent characteristics, for example,
using SSE instruction, ...

There's a concept error here. Both Java and C are fairly portable and
part of being portable is _not_ assuming that the underlying machine have
SSE, or 8-bit bytes, or whatever. Java doesn't do it, C doesn't do it.

A given compiler - say Intel's C compiler - may generate SSE instructions
for the compiled code, but that's specific to the particular compiler, not
the language. There's no reason a decent Java JIT compiler, for example,
couldn't do the same thing.

What you're really after, I suspect, is three things:

1) Decent profiling, to identify hot spots
2) Improved algorithms
3) When _absolutely_ necessary, tossing in a little ASM code

No reason you can't do 1 and 2 in Java, and 3 is completely irrelevant to
C; I suspect Java has a way to call routines in other languages. Not that
the third option is the one to use, generally.

On another note, you say " By the way, I am also pretty headache with
ArrayIndexOutOfBound exception" which suggests you're probably spending a
lot of time in exception handling overhead. Yes, C will "improve" things
here as it won't bother with the exception overhead - but the app will, as
a result, very likely produce incorrect results or simply crash. Fixing
the code which keeps going out of bounds would seem the simpler and
smarter option to speeding things up, even more so because those
exceptions suggest that the code has errors; fixing the errors may lead to
other improvements in speed, or at least correctness.
 
R

Rowland

Kelsey Bjarnason said:
1) Decent profiling, to identify hot spots
2) Improved algorithms
3) When _absolutely_ necessary, tossing in a little ASM code

No reason you can't do 1 and 2 in Java, and 3 is completely irrelevant to
C; I suspect Java has a way to call routines in other languages. Not that
the third option is the one to use, generally.

Yep - it's called the Java Native Interface (JNI), and it provides a
framework to call libraries written in other languages (although usually C,
as this is what it is geared towards). I've always found it works pretty
well, and it even allows two-way communication between the Java and C code.

Bit off-topic I suppose, but perhaps the OP could just use C for the parts
that are too slow.

Having said that, I think 99% of the time your algorithms are the place to
invest the effort. Java isn't THAT slow!
<SNIP>

Rowland.
 
K

Kelsey Bjarnason

[megasnip]

Having said that, I think 99% of the time your algorithms are the place to
invest the effort. Java isn't THAT slow!

I have - God's Honest Truth - seen people recoding a sort in assembly
because it ran too slow. A *bubblesort*. Hell, I'll use interpreted
BASIC and a merge and beat their hand-tweaked assembly bubblesort on any
significant data set.

The last time I can recall needing to go to assembly to get a performance
boost was many moons ago, using one of Borland's C compilers for DOS -
Turbo C, might have been Turbo C++, something of the era. I was doing
32-bit integer calculations but only cared about the lower 16 bits of thhe
final result. Using a couple lines of hand-coded assembler shaved off
enough overhead to to the job in about 1/10th the time of the compiled C
code - but this was more an artifact of doing _C_ integer math correctly
than anything specific to C or assembler; had my assembler code needed to
adhere to all the strict rules of C integer manipulation, it would
probably have been no faster.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top