Re: useful tips on how to write your C codes more efficiently
- From: "Mark F. Haigh" <mfhaigh@xxxxxxxxxxxxx>
- Date: 20 May 2005 00:59:04 -0700
eyh5@xxxxxxxxxxxxxxx wrote:
> Hi,
>
> I'm writing some C codes to run simulations.
> I'm wondering if there is a website that may contain useful
information
> on how to make one's code run more efficiently and in a
> computational-time-saving manner. Specifically, what I'd like to know
> is if there're any useful tips about writing your codes more
> efficiently.
1. Choose algorithms with appropriate complexity for the task at hand.
2. Code the algorithms simply and portably.
3. Compile the code with the appropriate optimization flags. Run it.
Is it fast enough? Probably. If it's not, then:
4. Profile the code. Identify the main bottlenecks.
5. Refactor the bottleneck code. Repeat #4 until no further gains are
made.
If it's still not fast enough, then:
6. Take advantage of platform-specific features. Isolate
platform-specific code.
7. Take advantage of CPU-specific instructions via inline assembly
code (which itself is not part of C, but a common extension). Again
isolate as much as possible.
> One such useful tip is that we can use the "switch"
> statement instead of multiple "if-else" statements; another is that
if
> you wish to put some information in an array, and that you foresee
the
> array is not going to be too big, then it probably pays to just
define
> a fixed size array instead of using "malloc."
You'd be surprised. On many fast modern machines, it won't make as big
of a difference as you think, unless you're executing it in a tight
loop. Write simple and idiomatic C code.
>
> So, I'm wondering if anybody knows of a website that may contain
> such helpful tips to help a mid-level programmer like me write codes
> better.
If your goal is to write C code better, simply read comp.lang.c daily,
code actively, and refactor relentlessly.
Mark F. Haigh
mfhaigh@xxxxxxxxxxxxx
.
- Follow-Ups:
- Re: useful tips on how to write your C codes more efficiently
- From: Anonymous 7843
- Re: useful tips on how to write your C codes more efficiently
- References:
- Prev by Date: Re: How two check struct size at compile time
- Next by Date: Re: create function at runtime
- Previous by thread: Re: useful tips on how to write your C codes more efficiently
- Next by thread: Re: useful tips on how to write your C codes more efficiently
- Index(es):
Relevant Pages
|
Loading