为什么我的C code运行慢? [英] Why does my C code run slow?

查看:198
本文介绍了为什么我的C code运行慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些C code和我很惊讶地看到它需要较长的时间来执行比我的预期。我想知道哪些操作是昂贵的,以及如何摆脱他们。

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.

有哪些常用的C性能缺陷的一些很好的参考?

What are some good references to common C performance pitfalls?

是否有一个良好分析器可以使用吗?

Is there a good profiler I can use?


谢谢大家

感谢您的输入。你是绝对正确:这是一种可以减缓东西(大大)算法。虽然有点性能增益可以通过编码实践来实现 - 我100%确信的只有故障算法可以大大事情慢下来

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.

作为事实上:我工作的RB树和升序插入节点。花了大量的时间(那么糟糕,因为二叉搜索树(斜))。寻求你的建议后,我检查,我取得了平衡的错误而把树斜塔(扭曲)的算法。我纠正它。

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.

您的建议,再次感谢。

推荐答案

您的性能问题可能有更多跟你比你使用的操作工具。这些算法

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

发布的code可能是有用的。告诉我们一些关于你想要做什么,以及你正在使用也将有助于算法。正因为如此,你的问题没有提供足够的信息,任何人都可以给你一个有用的答案。

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.

其他人推荐gprof的 - 我第二个,如果你感兴趣的分析您的code。我也用VTune™可视化之前,和喜欢它。但首先要确保你了解你的code和它做什么,那你实现算法的数据你期望它来处理规模打交道时是时间效率。

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.

顺便说一句,用C并不意味着你的code会自动运行得更快。 I / O密集​​型code会看到没有性能的提高,一般。 UI重code可能不会从使用低级语言中获益。通常情况下,C是你需要低级别的访问,与硬件或低级别的操作系统服务交互时,更好的实现语言,或者如果你有非常具体和严格的性能要求,这将是很难在一个较高水平,以满足,垃圾回收的语言。或者,如果你碰巧喜欢C,但是这显然是一个主观的问题。

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.

这篇关于为什么我的C code运行慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆