哪个循环在visual c ++中是最快和最好的 [英] which loop is fastest and best in visual c++

查看:121
本文介绍了哪个循环在visual c ++中是最快和最好的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个循环在c ++项目中最快且最好?以及如何?

Which loop is fastest and best in c++ projects? and how?

推荐答案

C ++ 中没有''最佳''循环。有三种循环:用于,而 do-while ,每一个都有利有弊(忘记相对表现)。根据我自己的经验,最常用的是

作为获得良好性能的一般规则,你需要

There is not a ''best'' loop in C++. There are three kinds of loop: for, while, do-while, each one with it pros and cons (forget about relative performance). In my own experience, the most used is for.
As a general rule to achieve good performance you have to
  1. 设计(或使用现有的)优秀算法。
  2. 掌握编程语言。





继续第(2)点你会注意到''最快循环''问题几乎没有意义。



Proceeding with point (2) you''ll notice that ''fastest loop'' questions make almost no sense.


最好的循环根本没有循环(搜索循环展开)。如果您想要多次并且非常快地执行某些操作,请考虑使用多个内核并行使用SSE扩展汇编程序指令并行执行。不要试图以这种方式做大块的程序,只需要适合的小数学数据。



一般来说优化是一个杯子游戏,除非在某些应用程序,视频处理和无疑渲染游戏项目,否则你不应该这样做,直到完成所有其他事情。



几乎所有有价值的优化都是一种权衡,例如使用更多的内存来获得更高的速度或者花费更长的时间但是使用更少的内存。你需要根据速度,内存,复杂性和编码工作来决定什么是重要的,你准备交易什么。
The best loop is no loop at all ( search loop unrolling ). If you want to do something several times and very fast then consider doing it in parallel using multiple cores or even using SSE extended assembler instructions. Don''t try to do large chunks of your program this way just the small expensive bits of mathematics that it''s suitable for.

In general optimization is a mugs game and you should not do it until everything else is done if at all but in certain applications, video processing and no doubt rendering for a game project it can be beneficial.

Almost all worthwhile optimizations are a tradeoff, for example use more memory to get more speed or take longer but use less memory. You need to decide what''s important and what you''re prepared to trade for it in terms of speed, memory, complexity and coding effort.


要考虑的一些事情:



首先,循环的速度通常取决于在循环类型内执行的代码。



其次,使用哪个循环取决于您希望控制循环的方式。您可以将任何类型的循环重写为另一种循环,但这不会改变您的要求和实现它们的努力! E. g。如果你知道你需要运行循环376次,那么你需要一个循环计数器,无论你是否使用进行循环。 OTOH,如果你的循环重复,直到值超过某个限制,执行循环比



第三,你甚至不知道会有瓶颈。也许代码运行速度比用户可以感知的快,无论你如何编程?在您知道存在需要它的问题之前,开始优化是浪费时间和精力的!



第四,调整代码只是为了让它运行得更快可能是弄巧成拙:现代编译器非常善于识别代码模式并对其进行优化。但是,如果您故意调整代码,可能会阻止编译器执行这些优化!



TL; DR

不要尝试优化,直到你编写代码,发现性能问题并确定问题为止瓶颈的原因!
Some things to consider:

First, the speed of a loop usually depends on the code performed within much more than the type of loop.

Second, which loop to use depends on the way you wish to control the loop. You can rewrite any type of loop as another, but that won''t change your requirements and efforts for fulfilling them! E. g. if you know you need to run a loop 376 times, then you need a loop counter, whether or not you use a for loop. OTOH, if your loop gets reiterated until a value exceeds a certain limit, a while or do loop makes more sense than for.

Third, you don''t even know there will be a bottleneck. Maybe the code runs faster than the user can perceive, no matter how you program it? It is wasted time and effort to start optimizing before you even know there is a problem that requires it!

Fourth, tweaking the code just for making it run faster may be self-defeating: modern compilers are extraordinarily good at recognizing code patterns and optimizing them. But if you deliberately tweak you code, that may prevent the compiler from performing these optimizations!

TL;DR
Don''t try to optimize until you''ve written the code, noticed a performance problem, and pinpointed the cause of the bottleneck!


这篇关于哪个循环在visual c ++中是最快和最好的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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