if(a& b)比if(a)if(b)需要更多的时间? [英] Should if(a&&b) take more time than if(a) if(b)?

查看:130
本文介绍了if(a& b)比if(a)if(b)需要更多的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下简单逻辑回答了这样的问题:

I used the following simple logic to answer a question like this one:

1:   if(a)        // 1 operation
2:   if (b)       // 1 operation

1:  if(a && b) // 1, 1(&&), 1 => 3 operations. 

因此,2个操作对3个操作,但是在第一个示例中,编译器需要调用另一条要执行的指令.

So, 2 operations versus 3, but in the first example the compiler needs to call another instruction to be executed.

此逻辑正确吗? 它取决于编译器吗? 调用仅像;这样的空指令是否会使编译器花费一些明显的时间?

Is this logic true?. Does it depend on the compiler?. Does calling an empty instruction like only ; cost the compiler some noticable time?.

也讨论了相同的问题,但未考虑此逻辑. 请帮助我们澄清这个问题.

This also discuss the same problem but not considering this logic. Please help us to clarify this issue.

推荐答案

有两种方法精确地来回答这样的问题:

There are two methods to answer such a question precisely:

1.)查看IL代码(和/或)汇编代码,并计算执行该代码所需的CPU周期(提示:这不适用于初学者)

1.) Look at the IL code (and/or) aassembly code produce and count the CPU cycles needed to execute this code (Hint: this is not for beginners)

2.)构建一个小的测试程序,该程序可以同时执行这两个变量的大量时间,请使用StopWatch()创建一个方便且可读的计时输出,并运行几次.

2.) Build a small test programm which executes both variants a large number of time, use StopWatch() to create a uesful and readable timing output, run it several times.

3.)推测您认为编译器的优化步骤能够执行的操作以及该软件将执行的操作,并与其他人争论数小时

3.) Speculate about what you think the optimization step of the compiler is able to do and what this software will do, argue with others for hours

这篇关于if(a& b)比if(a)if(b)需要更多的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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