类型转换是否会消耗cpu周期 [英] Does typecasting consume cpu cycles

查看:160
本文介绍了类型转换是否会消耗cpu周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码: -


无效*数据;

......

/ *数据初始化* /

......

struct known_struct * var =(struct known_struct *)data; / *类型转换* /


与简单赋值有什么不同。

int b = some_value;

int a = b ; / *赋值* /


上面的类型转换(struct known_struct *)是否需要一些额外的cpu周期???

或者它是一个简单的赋值,如'' a = b''。


感谢您的回复,

Vinod

解决方案

< blockquote>


Vinod Patel写道:

我有一段代码: -

void * data;
.....
/ *数据初始化* /
.....
struct known_struct * var =(struct known_struct *)data; / *类型转换* /

这与简单赋值有什么不同。
int b = some_value;
int a = b; / *赋值* /

上面的类型转换(struct known_struct *)是否需要一些额外的cpu周期???
或者它是一个简单的赋值,如'a = b''。

感谢您的回复,
Vinod



No.


作业本身可能需要一些时间(复制从一个

内存位置到另一个,或从一个寄存器到另一个寄存器的指针,或者

编译器可能会完全优化它),但那将是

分配,而不是演员,花时间。


演员在那里告诉编译器是的,那就是我真正想要的东西。 >
这样做STFU" ;.


(当然,这需要一些编译时间,因为演员需要解析,b $ b解析,但我认为你的意思是它是否消耗运行时CPU周期。)

Dave。


" Vinod Patel" < 6 ******** @ yahoo.co.in>在留言中写道

新闻:a3 ************************** @ posting.google.c om ...

我有一段代码: -

void * data;
.....
/ *数据初始化* /
.....
struct known_struct * var =(struct known_struct *)data; / *类型转换* /


指向void和其他指针类型之间的转换不需要

a cast。

这是怎么回事与简单赋值不同。
int b = some_value;
int a = b; / *赋值* /


区别在于第一种情况下类型不同,因此需要转换
。就像必须有一个转换,如果b是

声明上面的类型为char。

上面的类型转换(struct known_struct *)需要一些额外的cpu
周期吗? ??或者它是一个简单的任务,如''a = b''。




这取决于实施。总是存在转换,无论实现是否为b $ b,但转换可能是无操作(即,可能不会生成任何代码)。这适用于许多实现,其中所有指针

的大小相同并使用相同的表示。


Alex


On Tue,2004年9月14日02:36:58 -0700,Vinod Patel写道:

上面的类型转换(struct known_struct *)是否需要一些额外的cpu周期? ?




如果你必须知道,大多数编译器都可以选择将程序集打印成文件而不仅仅是目标代码。看看你的编译器的

文档。


另一方面,不要打扰。这几乎肯定不值得担心

。如果你需要从你的代码中榨取一些

的时间,请收紧一个循环或重新设计一些分支。


I have a piece of code : -

void *data;
......
/* data initialized */
......
struct known_struct *var = (struct known_struct*) data; /*typecasting*/

How is this different from simple assignment.
int b = some_value;
int a = b; /* assignment */

Does above typecasting (struct known_struct*) need some extra cpu cycles???
or is it a simple assignment like ''a = b''.

Thanks for reply,
Vinod

解决方案



Vinod Patel wrote:

I have a piece of code : -

void *data;
.....
/* data initialized */
.....
struct known_struct *var = (struct known_struct*) data; /*typecasting*/

How is this different from simple assignment.
int b = some_value;
int a = b; /* assignment */

Does above typecasting (struct known_struct*) need some extra cpu cycles???
or is it a simple assignment like ''a = b''.

Thanks for reply,
Vinod


No.

The assignment itself may take some time (copying the pointer from one
memory location to another, or from one register to another, or the
compiler might optimise it out altogether), but that would be the
assignment, not the cast, taking the time.

The cast is there to tell the compiler "yes, that''s really what I want
to do so STFU".

(It''ll take some compiler time, of course, because the cast needs
parsing, but I presume you mean "does it consume runtime CPU cycles".)

Dave.


"Vinod Patel" <vi********@yahoo.co.in> wrote in message
news:a3**************************@posting.google.c om...

I have a piece of code : -

void *data;
.....
/* data initialized */
.....
struct known_struct *var = (struct known_struct*) data; /*typecasting*/
Conversion between pointer to void and other pointer types does not require
a cast.
How is this different from simple assignment.
int b = some_value;
int a = b; /* assignment */
The difference is simply that the types are different in the first case, so
a conversion is required. Just like there must be a conversion if b was
declared with type char above.
Does above typecasting (struct known_struct*) need some extra cpu
cycles??? or is it a simple assignment like ''a = b''.



It depends on the implementation. There is always a conversion irrespective
of the implementation, but that conversion may be a no-op (ie, may not
generate any code). This applies to many implementations, where all pointers
are the same size and use the same representation.

Alex


On Tue, 14 Sep 2004 02:36:58 -0700, Vinod Patel wrote:

Does above typecasting (struct known_struct*) need some extra cpu cycles???



If you have to know, most compilers have an option of printing assembly
opcodes into a file instead of just object code. Look at your compiler''s
documentation.

On the other hand, don''t bother. It''s almost certainly not worth worrying
about. Tighten a loop or redesign some branches if you need to bum some
time out of your code.


这篇关于类型转换是否会消耗cpu周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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