9

最近、すべての計算サイクルが、ほとんどの最新のプロセッサと OS で 32 ビットまたは 64 ビットのマシン語で実行されることを知りました。Int16では、 、Int8、 のような小さなビットサイズの値を使用する利点は何Word8ですか? 彼らは正確には何のためにいるのですか?ストレージ削減のみですか?

いくつかのモジュールで構成されているが、値を返す単一の関数のみによってインターフェースされている複雑な計算プログラムを作成しているWord64ため、プログラム全体がWord64値になります。この質問への回答に興味があります。このプログラム内で、や などのさまざまIntegralなタイプを使用して小さなエンティティを表現していることに気づき、それらが頻繁に変換されるのを見て、次のように考えました。私が知らず知らずのうちに盲目的に惹かれたそれらのタイプの正確な利点は何でしたか? 他の整数型を利用して最終的にそれらを変換することはまったく意味がありましたか、それともどこでも使用する必要があったのでしょうか?Word16Word8fromIntegralfromIntegralWord64

4

2 に答える 2

6

These smaller types give you a memory reduction only when you store them in unboxed arrays or similar. There, each will take as many bits as indicated by the type suffix.

In general use, they all take exactly as much storage as an Int or Word, the main difference is that the values are automatically narrowed to the appropriate bit size when using fixed-width types, and there are (still) more optimisations (in the form of rewrite rules mainly) for Int and Word than for Int8 etc., so some operations will be slower using those.

Concerning the question whether to use Word64 throughout or to use smaller types, that depends. On a 64-bit system, when compiling with optimisations, the performance of Word and Word64 should mostly be the same since where it matters both should be unpacked and the work is done on the raw machine Word#. But there probably still are a few rules for Word that have no Word64 counterpart yet, so perhaps there is a difference after all. On a 32-bit system, most operations on Word64 are implemented via C calls, so there operations on Word64 are much slower than operations on Word.

So depending on what is more important, simplicity of code or performance on different systems, either

  1. use Word64 throughout: simple code, good performance on 64-bit systems
  2. use Word as long as your values are guaranteed to fit into 32 bits and transform to Word64 at the latest safe moment: more complicated code, but better performance on 32-bit systems.
于 2012-01-22T19:45:57.097 に答える