我应该使用,而不是与固定式大小LANGS 64位'诠释'(如Java,C#)“长” [英] Should I use 'long' instead of 'int' on 64-bits in langs with fixed type size (like Java, C#)

查看:151
本文介绍了我应该使用,而不是与固定式大小LANGS 64位'诠释'(如Java,C#)“长”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在10,甚至5年内将有没有[ EDIT2:服务器或桌面] 32位CPU。



那么,还有使用 INT (32位)超过(64位)?结果
和是否有使用 INT



什么坏处


编辑:




  1. 通过 10年或5年我的意思是在绝大多数地方都使用的那些LANGS


  2. 我的意思是哪种类型要使用默认即可。这天,我不会甚至懒得想,如果我要使用作为循环计数器,只是的for(int我... 。同样的道理专柜已经赢得


  3. 寄存器已64位,有已经是32位的类型没有收获。而且我觉得有些失落的8位类型(你有更多的位进行操作,那么你正在使用)



解决方案

如果你使用的是64位处理器,并且您已经编译了代码为64位,那么至少在某些时候, 很可能是更有效,因为它的寄存器大小相匹配,但是否会真正影响你的程序很多是有争议的。另外,如果你使用所有的地方,你通常要使用更多的内存 - 无论是在栈和堆 - 这可能会对性能产生负面影响有太多的变数肯定知道如何还有你的程序将执行使用默认情况下,而不是 INT 。还有原因可能是更快,原因可能是慢。这可能是一个共洗。



做典型的事情是只使用 INT 如果你不这样做关心的整数的大小。如果你需要一个64位整数,然后使用。如果你想使用较少的内存和 INT 远远超过你的需要,那么你用字节



x86_64的CPU都将被设计成在处理32位程序效率,所以它不是像使用 INT 将会严重降低性能。有些事情将会的,当你在64位CPU上使用64位整数更快由于更好的定位,但其他的东西会慢一些,由于增加的内存需求。而有可能是多种其他因素参与,这肯定会影响在任一方向的表现。



如果你真的想知道这是要为特定的应用程序做的更好在特定的环境中,你将需要配置文件吧。这不是那里是一个比其他的明显优势的情况下。



就个人而言,我会建议您遵循使用 INT 当你不关心整数的大小,当你使用其他类型的。


In 10, or even 5 years there will be no [Edit2: server or desktop] 32-bit CPUs.

So, are there any advantages in using int (32bit) over long (64bit) ?
And are there any disadvantages in using int ?


Edit:

  1. By 10 or 5 years I meant on vast majority of places where those langs are used

  2. I meant which type to use by default. This days I won't even bother to think if I should use short as cycle counter, just for(int i.... The same way long counters already win

  3. registers are already 64-bit, there is already no gain in 32 bit types. And I think some loss in 8 bit types (you have to operate on more bits then you're using)

解决方案

If you're on a 64-bit processor, and you've compiled your code for 64-bit, then at least some of the time, long is likely to be more efficient because it matches the register size. But whether that will really impact your program much is debatable. Also, if you're using long all over the place, you're generally going to use more memory - both on the stack and on the heap - which could negatively impact performance. There are too many variables to know for sure how well your program will perform using long by default instead of int. There are reasons why it could be faster and reasons why it could be slower. It could be a total wash.

The typical thing to do is to just use int if you don't care about the size of the integer. If you need a 64-bit integer, then you use long. If you're trying to use less memory and int is far more than you need, then you use byte or short.

x86_64 CPUs are going to be designed to be efficient at processing 32-bit programs and so it's not like using int is going to seriously degrade performance. Some things will be faster due to better alignment when you use 64-bit integers on a 64-bit CPU, but other things will be slower due to the increased memory requirements. And there are probably a variety of other factors involved which could definitely affect performance in either direction.

If you really want to know which is going to do better for your particular application in your particular environment, you're going to need to profile it. This is not a case where there is a clear advantage of one over the other.

Personally, I would advise that you follow the typical route of using int when you don't care about the size of the integer and to use the other types when you do.

这篇关于我应该使用,而不是与固定式大小LANGS 64位'诠释'(如Java,C#)“长”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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