它是更好地使用整数作为循环计数器变量? [英] Is it better to use integers as loop counter variables?

查看:82
本文介绍了它是更好地使用整数作为循环计数器变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得读书的地方,最好是用整数作为循环计数器变量,而不是char或短。如果是,为什么?它是否提供任何优化的好处?

I remember reading somewhere that it is better to use integers as loop counter variables rather than char or short. If yes, why? Does it provide any optimization benefits?

推荐答案

一般来说,编译器将 INT 是一个很好的大小,投入一个你的CPU的通用通用寄存器。这通常导致的快速访问。

Generally, the compiler will make int to be a good size for putting into one of your CPU's general purpose registers. That generally leads to fast access.

当然,没有任何东西保证。编译器是免费做了很多事情,包括我猜,促进了一些使用字符来一些较大型的code的。因此,差异甚至可能没有关系。

Of course there's no guarantee of anything. The compiler is free to do a lot of things, including, I would guess, promote some of your code that uses char to some larger type. So the difference might not even matter.

说真的,对于为是真实的答案 的编译器,你应该看看它输出的程序集。

Really, for the answer that's true for your compiler, you should look at the assembly it outputs.

这篇关于它是更好地使用整数作为循环计数器变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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