With CPUs being increasingly faster, hard disks spinning, bits flying around so quickly, network speeds increasing as well, it's not that simple to tell bad code from good code like it used to be.
随着CPU越来越快,硬盘在旋转,比特飞得如此之快,网络速度也越来越快,从过去的好代码中判断坏代码并不是那么简单。
I remember a time when you could optimize a piece of code and undeniably perceive an improvement in performance. Those days are almost over. Instead, I guess we now have a set of rules that we follow like "Don't declare variables inside loops" etc. It's great to adhere to these so that you write good code by default. But how do you know it can't be improved even further without some tool?
我记得有一段时间你可以优化一段代码,并且无可否认地看到了性能的提升。那些日子快结束了。相反,我想我们现在有一套规则,我们遵循“不要在循环中声明变量”等。坚持这些是很好的,这样你就可以默认编写好的代码了。但是你怎么知道如果没有一些工具它就无法进一步改进?
Some may argue that a couple of nanoseconds won't really make that big a difference these days. The truth is, we are stuck with so many layers that you get a staggering effect.
有些人可能会争辩说,这几天几纳秒不会真正发挥重要作用。事实是,我们坚持这么多层,你会得到惊人的效果。
I'm not saying we should optimize every little millisecond out of our code as that will be expensive and unfeasible. I believe we have to do our best, given our time constraints, to write efficient code as well.
我并不是说我们应该优化我们代码中的每一毫秒,因为这将是昂贵且不可行的。我认为,考虑到我们的时间限制,我们必须尽力编写有效的代码。
I'm just interested to know what tools you use to profile and measure performance of code, if at all.
我只是想知道您使用什么工具来分析和衡量代码的性能(如果有的话)。
6 个解决方案
#1
There's a big difference between "good" code and "fast" code. They aren't exactly separate from each other either, but "fast" code doesn't mean "good". Often times, "fast" actually means bad code because readability compromises must be made to make it fast.
“好”代码和“快速”代码之间存在很大差异。它们也不完全相互独立,但“快速”代码并不意味着“好”。通常,“快速”实际上意味着代码不好,因为必须做出可读性妥协才能使其快速进行。
The way I look at it, hardware is cheap, programmers are expensive. Unless there is a serious performance problem with some piece of code, you should never have to worry about speed. If there are performance problems, you'll notice them. Only when you notice the performance problem on good hardware should you have to worry about optimization (in my opinion)
我看待它的方式,硬件便宜,程序员很贵。除非某些代码存在严重的性能问题,否则您永远不必担心速度问题。如果存在性能问题,您会注意到它们。只有当您注意到良好硬件上的性能问题时,您才需要担心优化(在我看来)
If you reach the point where your code is slow, but you can't figure out why, I'd use a profiler like ANT, or dotTrace if you're in the .NET world (I'm sure there are others out there for other platforms & languages). They're pretty useful, but I've only ever had one situation where I needed a profiler to identify the problem. It was something that now that I know the issue, I won't need a profiler again to tell me it's a problem because I'll never forget the amount of time I spent trying to optimize it.
如果你的代码很慢,但你无法弄清楚原因,我会使用像ANT这样的分析器,或者如果你在.NET世界中使用dotTrace(我确定那里还有其他人)对于其他平台和语言)。它们非常有用,但我只有一种情况需要一个分析器来识别问题。现在我知道这个问题,我不再需要一个分析器来告诉我这是一个问题,因为我永远不会忘记我花在试图优化它上面的时间。
#2
I think that optimization should be thought of not as looking at each line of code, but rather, what asymptotic complexity is your algorithm. For example, using a bubble sort is probably one of the worst sorting algorithms you could use in terms of optimization. It takes the longest. Quicksort and mergesort are faster in terms of sorting, and should be always used before a bubble sort.
我认为优化应该被认为不是看每行代码,而是渐进复杂度是你的算法。例如,使用冒泡排序可能是您在优化方面可以使用的最差排序算法之一。它花费的时间最长。 Quicksort和mergesort在排序方面更快,并且应该在冒泡排序之前始终使用。
If you keep optimization always in your mind when designing a solution to a problem, then you should be able to write readable code, which other developers will approve of. Also, if you are programming in a higher level language that will be compiled before it is run, remember that compilers make some awesome optimizations nowadays that you or I may not think of, and also (more importantly) do not have to worry about.
如果在设计问题的解决方案时始终牢记优化,那么您应该能够编写可读代码,其他开发人员将批准这些代码。此外,如果您使用将在运行之前编译的更高级语言进行编程,请记住编译器现在进行一些非常棒的优化,您或我可能没有想到,并且(更重要的是)不必担心。
Stick with a good and low big O(), and it should be optimized pretty good. If you are working with millions or greater in some type of dataset, then look for a big O(logn) algorithm. They work great for large tasks, and keep your code optimized.
坚持使用优秀的低O(),它应该优化得很好。如果您在某种类型的数据集中使用数百万或更多,那么请寻找一个大的O(logn)算法。它们适用于大型任务,并保持代码优化。
Let the compilers work on the line by line code optimizations so you can focus on the solutions.
让编译器逐行优化,以便您可以专注于解决方案。
There are times that do warrant line by line optimizations, and if that is the case that you need that much speed, maybe you might want to look into assembly so that you can control every line that is written.
有些时候需要逐行优化,如果你需要那么快的速度,也许你可能想要查看汇编,这样你就可以控制所写的每一行。
#3
This is absolutely a valid concern, but not for most developers. Most developers are concerned with getting a product that works to their employer. Optimized code is seldom a requirement.
这绝对是一个有效的问题,但不适用于大多数开发人员。大多数开发人员都关心如何获得适合其雇主的产品。优化的代码很少是必需的。
The best way to make sure your code is fast is to benchmark or profile it. A lot of compiler optimizations create non-intuitive oddities in the performance of a programmer's code, so in the end measurement becomes essential.
确保代码快速的最佳方法是对其进行基准测试或分析。许多编译器优化会在程序员代码的性能中产生非直观的奇怪现象,因此最终测量变得至关重要。
#4
In my experience, Rational Quantify has given me the best results in terms of code tuning. It is not free, but it is very fully featured and seems to have given me the most useful results.
根据我的经验,Rational Quantify在代码调优方面给了我最好的结果。它不是免费的,但功能非常全面,似乎给了我最有用的结果。
In terms of free tools, check out gprof or oprofile, if you are on a Unix environment. They are not as good as some of the commercial tools, but can often point you in the right direction.
在免费工具方面,如果您使用的是Unix环境,请查看gprof或oprofile。它们不如某些商业工具好,但通常可以指向正确的方向。
On a side note, I am almost always surprised at what profilers turn up the first time I use them. You can have intuition as to where code may be bottlenecking, and it can often be completely wrong.
在旁注中,我几乎总是惊讶于第一次使用它们时轮廓仪出现了什么。您可以直观地了解代码可能存在瓶颈的地方,而且通常可能完全错误。
#5
Almost all code I write is plenty fast enough. On the rare occasions when it isn't, for C, C++, and Objective Caml I use the venerable gprof
and the excellent valgrind
with its superb visualizer kcachegrind
(part of the KDE SDK; don't be fooled by the out-of-date code on sourceforge).
我写的几乎所有代码都足够快。在极少数情况下,对于C,C ++和Objective Caml,我使用古老的gprof和优秀的valgrind及其出色的可视化工具kcachegrind(KDE SDK的一部分;不要被以外的人愚弄) sourceforge上的日期代码)。
The MLton Standard ML compiler and the Glasgow Haskell Compiler both ship with excellent profilers.
MLton Standard ML编译器和Glasgow Haskell编译器都配有优秀的分析器。
I wish there were a better profiler for Lua.
我希望有一个更好的Lua剖析器。
#6
Uh, a profiler maybe? There are ones available for almost all platforms and languages.
呃,探险家可能吗?有几种可用于几乎所有平台和语言。
#1
There's a big difference between "good" code and "fast" code. They aren't exactly separate from each other either, but "fast" code doesn't mean "good". Often times, "fast" actually means bad code because readability compromises must be made to make it fast.
“好”代码和“快速”代码之间存在很大差异。它们也不完全相互独立,但“快速”代码并不意味着“好”。通常,“快速”实际上意味着代码不好,因为必须做出可读性妥协才能使其快速进行。
The way I look at it, hardware is cheap, programmers are expensive. Unless there is a serious performance problem with some piece of code, you should never have to worry about speed. If there are performance problems, you'll notice them. Only when you notice the performance problem on good hardware should you have to worry about optimization (in my opinion)
我看待它的方式,硬件便宜,程序员很贵。除非某些代码存在严重的性能问题,否则您永远不必担心速度问题。如果存在性能问题,您会注意到它们。只有当您注意到良好硬件上的性能问题时,您才需要担心优化(在我看来)
If you reach the point where your code is slow, but you can't figure out why, I'd use a profiler like ANT, or dotTrace if you're in the .NET world (I'm sure there are others out there for other platforms & languages). They're pretty useful, but I've only ever had one situation where I needed a profiler to identify the problem. It was something that now that I know the issue, I won't need a profiler again to tell me it's a problem because I'll never forget the amount of time I spent trying to optimize it.
如果你的代码很慢,但你无法弄清楚原因,我会使用像ANT这样的分析器,或者如果你在.NET世界中使用dotTrace(我确定那里还有其他人)对于其他平台和语言)。它们非常有用,但我只有一种情况需要一个分析器来识别问题。现在我知道这个问题,我不再需要一个分析器来告诉我这是一个问题,因为我永远不会忘记我花在试图优化它上面的时间。
#2
I think that optimization should be thought of not as looking at each line of code, but rather, what asymptotic complexity is your algorithm. For example, using a bubble sort is probably one of the worst sorting algorithms you could use in terms of optimization. It takes the longest. Quicksort and mergesort are faster in terms of sorting, and should be always used before a bubble sort.
我认为优化应该被认为不是看每行代码,而是渐进复杂度是你的算法。例如,使用冒泡排序可能是您在优化方面可以使用的最差排序算法之一。它花费的时间最长。 Quicksort和mergesort在排序方面更快,并且应该在冒泡排序之前始终使用。
If you keep optimization always in your mind when designing a solution to a problem, then you should be able to write readable code, which other developers will approve of. Also, if you are programming in a higher level language that will be compiled before it is run, remember that compilers make some awesome optimizations nowadays that you or I may not think of, and also (more importantly) do not have to worry about.
如果在设计问题的解决方案时始终牢记优化,那么您应该能够编写可读代码,其他开发人员将批准这些代码。此外,如果您使用将在运行之前编译的更高级语言进行编程,请记住编译器现在进行一些非常棒的优化,您或我可能没有想到,并且(更重要的是)不必担心。
Stick with a good and low big O(), and it should be optimized pretty good. If you are working with millions or greater in some type of dataset, then look for a big O(logn) algorithm. They work great for large tasks, and keep your code optimized.
坚持使用优秀的低O(),它应该优化得很好。如果您在某种类型的数据集中使用数百万或更多,那么请寻找一个大的O(logn)算法。它们适用于大型任务,并保持代码优化。
Let the compilers work on the line by line code optimizations so you can focus on the solutions.
让编译器逐行优化,以便您可以专注于解决方案。
There are times that do warrant line by line optimizations, and if that is the case that you need that much speed, maybe you might want to look into assembly so that you can control every line that is written.
有些时候需要逐行优化,如果你需要那么快的速度,也许你可能想要查看汇编,这样你就可以控制所写的每一行。
#3
This is absolutely a valid concern, but not for most developers. Most developers are concerned with getting a product that works to their employer. Optimized code is seldom a requirement.
这绝对是一个有效的问题,但不适用于大多数开发人员。大多数开发人员都关心如何获得适合其雇主的产品。优化的代码很少是必需的。
The best way to make sure your code is fast is to benchmark or profile it. A lot of compiler optimizations create non-intuitive oddities in the performance of a programmer's code, so in the end measurement becomes essential.
确保代码快速的最佳方法是对其进行基准测试或分析。许多编译器优化会在程序员代码的性能中产生非直观的奇怪现象,因此最终测量变得至关重要。
#4
In my experience, Rational Quantify has given me the best results in terms of code tuning. It is not free, but it is very fully featured and seems to have given me the most useful results.
根据我的经验,Rational Quantify在代码调优方面给了我最好的结果。它不是免费的,但功能非常全面,似乎给了我最有用的结果。
In terms of free tools, check out gprof or oprofile, if you are on a Unix environment. They are not as good as some of the commercial tools, but can often point you in the right direction.
在免费工具方面,如果您使用的是Unix环境,请查看gprof或oprofile。它们不如某些商业工具好,但通常可以指向正确的方向。
On a side note, I am almost always surprised at what profilers turn up the first time I use them. You can have intuition as to where code may be bottlenecking, and it can often be completely wrong.
在旁注中,我几乎总是惊讶于第一次使用它们时轮廓仪出现了什么。您可以直观地了解代码可能存在瓶颈的地方,而且通常可能完全错误。
#5
Almost all code I write is plenty fast enough. On the rare occasions when it isn't, for C, C++, and Objective Caml I use the venerable gprof
and the excellent valgrind
with its superb visualizer kcachegrind
(part of the KDE SDK; don't be fooled by the out-of-date code on sourceforge).
我写的几乎所有代码都足够快。在极少数情况下,对于C,C ++和Objective Caml,我使用古老的gprof和优秀的valgrind及其出色的可视化工具kcachegrind(KDE SDK的一部分;不要被以外的人愚弄) sourceforge上的日期代码)。
The MLton Standard ML compiler and the Glasgow Haskell Compiler both ship with excellent profilers.
MLton Standard ML编译器和Glasgow Haskell编译器都配有优秀的分析器。
I wish there were a better profiler for Lua.
我希望有一个更好的Lua剖析器。
#6
Uh, a profiler maybe? There are ones available for almost all platforms and languages.
呃,探险家可能吗?有几种可用于几乎所有平台和语言。