Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

performance - Why does my C code run slow?

I wrote some code in C and I was surprised to see it take a longer time to execute than I expected. I want to know which operations are costly and how to get rid of them.

I use assignment statements, conditional(nested), loops, function calls and call backs.

What are some good references to common C performance pitfalls?

Is there a good profiler I can use?


Thank you all

Thanks for all your inputs. You are absolutely right: It's algorithms that can slow things (drastically). Though a little performance gain can be achieved by coding practices - I am 100% convinced that only faulty algorithm can drastically slow down things.

As a matter of fact: I was working on RB trees and inserting nodes in ascending order. It took enormous time (as bad as Binary Search Tree (Skewed) ). After seeking your advice I checked the algorithm where I made a mistake in balancing which made the tree leaning (skewed). I corrected it.

Thanks once again for the suggestions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your performance issues probably have more to do with the algorithms you implement than with the operations you use.

Posting the code might be useful. Telling us something about what you're trying to do, and what algorithm you're using would also help. As it is, your question doesn't provide enough information for anyone to give you a helpful answer.

Other people recommended gprof - I second that, if you're interested in profiling your code. I've also used VTune before, and liked it. But first make sure you understand your code and what it does, and that the algorithm you're implementing is time efficient when dealing with the size of data you expect it to handle.

As an aside, using C doesn't mean that your code would automatically run faster. I/O bound code would see no performance improvement, typically. UI heavy code might not benefit from using a low level language. Typically, C is a better implementation language where you need low level access, when interfacing with hardware or low level operating system services, or if you have very specific and stringent performance requirements that would be difficult to meet in a high level, garbage collected language. Or if you happen to like C, but that's obviously a subjective matter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...