简体   繁体   English

printf性能:最好使用“%d”或“%c”表示单位数字

[英]printf performance: Better to use “%d” or “%c” for single digit numbers

I am printing information about a struct which only contains information which will all be a single digit (small indices and char which act as bools mostly). 我正在打印有关结构的信息,该结构仅包含将成为单个数字的信息(小索引和char主要充当bool)。

I want my printf calls to be as efficient as possible. 我希望我的printf调用尽可能高效。 So would it be better to use the normal %i / %d format specifiers, or is it better to use %c and add 0x30 ( '0' ) to the number, since then there shouldn't need to be any formatting for printf to do? 那么使用普通的%i / %d格式说明符会更好'0' ,或者更好的是使用%c并在数字中添加0x30'0' ),因为那时不需要对printf进行任何格式化去做?

Edit/Clarification: I have profiled some small programs which try both techniques, and the %c can be faster. 编辑/澄清:我已经介绍了一些尝试这两种技术的小程序, %c可以更快。 If this were for production code, I would just stick to the %d , but this is specifically for speed-based competitions. 如果这是生产代码,我会坚持%d ,但这是专门针对基于速度的比赛。 (sorry because that wasn't clear before). (对不起,因为之前不清楚)。

The only way to know for sure is to code up both versions and profile them (or compare the generated machine code). 确切知道的唯一方法是对两个版本进行编码并对其进行分析(或比较生成的机器代码)。 Having said that, I doubt you'd see much difference in runtime performance. 话虽如此,我怀疑你会发现运行时性能有很大差异。 Modern compilers are smart, and standard library implementations are tuned to be as efficient as possible. 现代编译器是智能的,标准库实现被调整为尽可能高效。 It's quite likely that your printf implementation already has special case handling for single-digit integers that does something similar to what you are proposing. 很可能你的printf实现已经对单位数整数进行了特殊的案例处理,这与你提出的类似。 And all of that's down in the noise compared to the overhead of just calling printf in the first place. 与首先调用 printf的开销相比,所有这些都在噪声中。

Code should be, in order of importance: 代码应按重要性顺序排列:

  1. Correct - it does everything it is supposed to do, and nothing it isn't supposed to do; 正确 - 它完成它应该做的一切, 而不应该做任何事情;
  2. Robust - it shouldn't fall over and die if somebody looks at it funny; 坚固 - 如果有人看起来很有趣,它不应该摔倒并死亡;
  3. Secure - it's not a malware vector, it doesn't expose any data it shouldn't; 安全 - 它不是恶意软件向量,它不会暴露任何不应该的数据;
  4. Maintainable - some poor schmuck is eventually going to have to change this code to add functionality (or fix a defect); 可维护 - 一些可怜的schmuck最终将不得不更改此代码以添加功能(或修复缺陷);
  5. Efficient - it shouldn't take any more space or time than necessary to accomplish a given task, which is often driven by choice of algorithm and data structures more than anything specific micro-optimization. 高效 - 它不应占用完成给定任务所需的任何空间或时间,这通常是通过选择算法和数据结构而不是任何特定的微优化来驱动的。

I'd argue that your approach makes your code less maintainable, because it's a non-obvious way to achieve a given result. 我认为你的方法使你的代码不易维护,因为这是实现给定结果的一种非显而易见的方法。

Since you are printing integer numbers, use %d , which improves readability. 由于您要打印整数,请使用%d ,这样可以提高可读性。

Using the one over the other won't affect performance in any significant way. 使用其中一个不会以任何显着的方式影响性能。

If your application's bottleneck is really the printing phase, then considering using another approach than printf() . 如果您的应用程序的瓶颈确实是打印阶段,那么考虑使用另一种方法而不是printf() Some ideas: use fwrite() , optimize stdout , or POSIX write() , as @YoYoYonnY suggested. 一些想法: 使用fwrite()优化stdout或POSIX write() ,如@YoYoYonnY建议的那样。

In general, printing in stdout is relatively slow, in comparison to doing computations, for example... 通常,与计算相比,stdout中的打印相对较慢,例如......

HyperTip: Premature optimization is the root of evil - D. Knuth. HyperTip:过早优化是邪恶的根源 - D. Knuth。

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

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