对于大矩阵,int与bool的速度 [英] speed of int vs bool for large matrix

查看:367
本文介绍了对于大矩阵,int与bool的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我看到它提到了int是在b

中使用最快的数据类型,如果我在

中使用int,那么数据存储/检索将是最快的32位以下4种情况4字节整数的机器:


int m [1000] [1000];

bool m [1000] [1000]; //假设我使用C ++

char m [1000] [1000];

unsigned char m [1000] [1000];


我需要知道这个陈述是否属实。另外,如果int确实是最快的,那么在实践中,bool矩阵的速度会慢多少?如果我必须动态分配上述所有4个案例,那么该怎么回事?同样的

陈述是否仍然存在?


其实我是需要一个大的矩阵来存储一个二进制值(所以

bool是最合适的) - 但速度是我最关心的,空间

需要接下来。


一些指示?


提前致谢。

Rajorshi

Hi,
I saw it mentioned that "int" is the fastest data-type for use in C
,that is data storage/retrieval would be the fastest if I use int among
the following 4 situations in a 32 bit machine with 4-byte ints:

int m[1000][1000];
bool m[1000][1000]; // assuming I use C++
char m[1000][1000];
unsigned char m[1000][1000];

I need to know if this statement is true. Also, if int is indeed the
fastest, in practice, how slower would the bool matrix be ? What if I
had to dynamically allocate all the 4 cases above - would the same
statements still hold?

Actually I need a large matrix for storing just a binary value (so
bool would be most appropriate) - but speed is my utmost concern, space
required comes next.

Some pointers ?

Thanks in advance.
Rajorshi

推荐答案

raj写道:


我看到它提到了int是在C
中使用的最快的数据类型我需要知道这个陈述是否属实。

Hi,
I saw it mentioned that "int" is the fastest data-type for use in C I need to know if this statement is true.




这几乎是真的。

这就是C的规则实际上说的:


N869

6.2.5类型

[# 5]

一个``plain''''int对象具有执行环境的

架构所建议的自然大小(足够大到
$ b) $ b包含定义的INT_MIN到INT_MAX范围内的任何值
标题中的
< limits.h>)。


-

pete



It''s almost true.
This is what the rules of C actually say:

N869
6.2.5 Types
[#5]
A ``plain'''' int object has the natural size suggested by the
architecture of the execution environment (large enough to
contain any value in the range INT_MIN to INT_MAX as defined
in the header <limits.h>).

--
pete


raj写道:

我看到它提到了int是在C
中使用的最快的数据类型,即如果我在以下4个字节的32位机器中的以下4种情况中使用int,则数据存储/检索将是最快的:

int m [1000] [1000];
bool m [1000] [1000]; //假设我使用C ++
char m [1000] [1000];
unsigned char m [1000] [1000];

我需要知道这个陈述是否为真。另外,如果int确实是最快的,实际上,bool矩阵会有多慢?如果我必须动态分配上述所有4个案例,那么相同的
语句是否仍然存在?

实际上我需要一个大矩阵来存储一个二进制值(所以
bool是最合适的) - 但速度是我最关心的,接下来需要空间。

一些指示?

提前致谢。
Rajorshi
Hi,
I saw it mentioned that "int" is the fastest data-type for use in C
,that is data storage/retrieval would be the fastest if I use int among
the following 4 situations in a 32 bit machine with 4-byte ints:

int m[1000][1000];
bool m[1000][1000]; // assuming I use C++
char m[1000][1000];
unsigned char m[1000][1000];

I need to know if this statement is true. Also, if int is indeed the
fastest, in practice, how slower would the bool matrix be ? What if I
had to dynamically allocate all the 4 cases above - would the same
statements still hold?

Actually I need a large matrix for storing just a binary value (so
bool would be most appropriate) - but speed is my utmost concern, space
required comes next.

Some pointers ?

Thanks in advance.
Rajorshi




如果您将表声明为无符号字符表,则内存
requitrements将为1 000 000字节,在另一种情况下将是b
$(假设一台32位机器),即有4倍

的数据将适合1级缓存。


输入输出要求将大大降低,

计划肯定会快得多。


如果你有一个纯粹的布尔矩阵,我会使用8位/字节的打包

表示,使你的矩阵只需要
125K,即比使用int数据类型。


请注意,减少空间需求会提高速度,因为从RAM填充缓存的
引入了大量的等待

表示。内存访问通过总线在300 MHZ,一个CPU运行在3 000 MHZ的
,即内存访问速度可能慢10倍

当它没有点击L1缓存。


当然,这种分析意味着可以顺序访问矩阵的每个

元素。在其他情况下,您只需访问矩阵的随机元素,因为您不受I / O限制,因此整数表示将会比其他所有其他元素更好。


主要点

----------

推测速度永远不会取代实际测量值。


1:构建测试矩阵。

2:模拟应用程序的访问模式。

3:使用unsigned char,int计算时间。


然后你肯定会知道的。


jacob



If you declare your table as a table of unsigned chars, memory
requitrements will be 1 000 000 bytes, in the other case will be
4 times as much (supposing a 32 bit machine), i.e. there is 4 times
as much data that will fit the Level 1 cache.

Input output requirements will be much more reduced and the
program will be surely much faster.

If you have a purely boolean matrix, I would use a packed
representation with 8 bits/byte, making your matrix just
125K, i.e. 8 times less than if you use the int data type.

Note that reducing space requirements increases speed, since
filling the cache from RAM introduces an enormous amount of wait
states. Memory access go through the bus at 300 MHZ, with a CPU
running at 3 000 MHZ, i.e. a memory access can be 10 times slower
when it doesn''t hit the L1 cache.

Of course this analysis implies a sequential access to each
element of the matrix. In other cases where you just access
randomly elements of the matrix, an integer representation would
beat all others since you are NOT limited by I/O.

MAIN POINT
----------
Speculating about speed will never replace actual measurements.

1: Build a test matrix.
2: Simulate the access pattern of your application.
3: Time it using unsigned char, int.

Then you will know for sure.

jacob


" raj" ; < RA ****** @ fastmail.fm>写道:



#我看到它提到了int是用于C的最快数据类型

这是错的。 MU的正确答案是。假设你正在解决偏微分方程;最快的

数据类型是float还是double。你可以在int中使用各种整数来实现它。

保持指数和分数,但是使用什么

编码的时间要快得多硬件和数学库。什么是最快的取决于

操作执行什么数据以什么顺序在哪些硬件上使用什么缓存

以及什么样的虚拟内存(如果有的话)管理。没有普遍的

正确答案。


-

SM瑞恩 http://www.rawbw.com/~wyrmwif/

谁''领导这些暴徒?
"raj" <ra******@fastmail.fm> wrote:
# Hi,
# I saw it mentioned that "int" is the fastest data-type for use in C

That''s wrong. The correct answer is MU. Suppose you''re solving PDEs; the fastest
data type is float or double. You can do it in int, using various integers to
hold the exponents and fractions, but it''s going to be a lot faster using what
is encoded in the hardware and math libraries. What is fastest depends on the
operations performed on what data in what order on what hardware with what caches
and what kind of virtual memory (if any) management. There''s is no universally
correct answer.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who''s leading this mob?


这篇关于对于大矩阵,int与bool的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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