简体   繁体   English

在C / C ++中根据条件分配一行

[英]One line assignment according condition in c/c++

Hi, 你好

i would like to know which one of these assignments is faster, safer, better etc. and possibly why: 我想知道这些作业中的哪一个更快,更安全,更好等等,以及可能的原因:

int choice = fgetc(stdin);
unsigned int bSize;

choice = fgetc(stdin)

1: 1:

bSize = (choice == 'y' || choice == 'Y') ? 256 : 128;

2: 2:

bSize = 128 + ((choice == 'y' || choice == 'Y') << 7);

Thanks. 谢谢。

Regarding speed, this would be at least as fast as choice 2: 关于速度,这至少与选择2一样快:

bSize = 128 << (choice == 'y' || choice == 'Y');

Whether that would be faster than choice 1 is not immediately obvious to me. 对于我来说这是否会比选择1更快还不是很明显。 However, for tuned performance on an unknown platform, I think that I like the suggested variant on choice 2. The reason is that, at the hardware level, choice 2 (original or variant) does not involve reloading the program counter, but invokes a relatively straightforward shift-register operation, involving relatively few transistors. 但是,为了在未知平台上调整性能,我认为我喜欢选择2的建议变体。原因是,在硬件级别,选择2(原始或变体)不涉及重新加载程序计数器,而是调用一个相对简单的移位寄存器操作,涉及相对较少的晶体管。 (Actually, if you want to get really technical about it, I am given to understand that the shift is probably accomplished by multiplexing. To detail that would be too much for the present format, but the point is that the output of (choice == 'y' || choice == 'Y') is effectively piped straight to one of the multiplexer's control lines. Anyway, it's really fast.) (实际上,如果您想真正地了解它,可以理解的是,这种移位很可能是通过多路复用完成的对于当前的格式来说,这太过分了,但是重点是(choice == 'y' || choice == 'Y')有效地直接管道传输到多路复用器的控制线之一。无论如何,这确实非常快。

Regarding whether one can safely use the evaluated condition in the manner suggested, ISO/IEC 9899:1999 (E), sect. 关于是否可以按照建议的方式安全使用评估条件,ISO / IEC 9899:1999(E),第1节。 6.5.14.3, guarantees that one can safely do this. 6.5.14.3保证可以安全地执行此操作。 It reads, "The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int." 它显示为:“ ||运算符如果两个操作数中的任何一个比较不等于0,则将产生1;否则,它将产生0。结果的类型为int。”

(@PaulR rightly observes that electronic-theoretical considerations like the ones this answer offers are not decisive. One must profile actual code on an actual platform to tell for sure which is faster. Nor is this a mere quibble on @PaulR's part. It is all well to argue that choice 2 would be faster, but this does not mean that is is faster. Depending on the CPU in use, branch-predicting or other hardware could promote choice 1, nor would I be extremely surprised if it did.) (@PaulR正确地观察到像该答案所提供的那样的电子理论考虑因素不是决定性的。必须在实际平台上分析实际代码以确保更快地运行。这也不是@PaulR方面的一个小问题。它是所有井认为,选择2 更快,但这并不意味着是速度更快 。根据所使用的CPU上,分支预测或其他硬件可以促进首选1,也不会我,如果它确实是非常惊讶。)

In my opinion 'Choice 1' is 'faster' because there's only one assignment operation done after the comparison. 我认为“选择1”是“更快的”,因为在比较之后仅执行了一次分配操作。 In 'Choice 2' it does '+' and '<<' along with the comparison part. 在“选择2”中,它与比较部分一起执行“ +”和“ <<”。 'Choice 1' is 'safer' because it is more readable than the other choice so programmer will have less chance to do error in writing it. “选择1”更“安全”,因为它比其他选择更具可读性,因此程序员在编写它时出错的机会会更少。 Choice 1 is 'better' because of the previous two reasons. 由于前面两个原因,选择1比较“好”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM