Re: useful tips on how to write your C codes more efficiently



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

.



Relevant Pages

  • Re: useful tips on how to write your C codes more efficiently
    ... > I'm writing some C codes to run simulations. ... > you wish to put some information in an array, ... I'm wondering if anybody knows of a website that may contain ...
    (comp.lang.c)
  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • Re: problem reading/writing structures from and to files
    ... My progam can write the Data array into a binary file, after writing ... FILE *somefile; ... although the initial fwrite() -- writing bytes ...
    (comp.lang.c)
  • Re: Read and re-write file with one open?
    ... This opens a file for reading and writing, reads the file into an array, ... altered data back to the file, and the file contains half altered data ...
    (comp.lang.ruby)

Loading