发送字节数到标准输出C / C ++最好的方法 [英] C/C++ best way to send a number of bytes to stdout

查看:197
本文介绍了发送字节数到标准输出C / C ++最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的概要分析程序和打印功能是服用大量的时间来执行。如何我可以发送原始的字节直接输出到标准输出,而不是使用FWRITE,并使其更快(需要发送的所有9bytes在打印(),同时向标准输出)?

Profiling my program and the function print is taking a lot of time to perform. How can I send "raw" byte output directly to stdout instead of using fwrite, and making it faster (need to send all 9bytes in the print() at the same time to the stdout) ?

void print(){
    unsigned char temp[9];

    temp[0] = matrix[0][0];
    temp[1] = matrix[0][1];
    temp[2] = matrix[0][2];
    temp[3] = matrix[1][0];
    temp[4] = matrix[1][1];
    temp[5] = matrix[1][2];
    temp[6] = matrix[2][0];
    temp[7] = matrix[2][1];
    temp[8] = matrix[2][2];

    fwrite(temp,1,9,stdout);

}

矩阵全局定义为一个无符号字符矩阵[3] [3];

Matrix is defined globally to be a unsigned char matrix[3][3];

推荐答案

IO是不是一种廉价的操作。这是,事实上,一个阻止运行,这意味着操作系统可以preempt你的过程,当你调用来让更多的CPU-绑定进程运行,你写来完成操作的IO设备之前。

IO is not an inexpensive operation. It is, in fact, a blocking operation, meaning that the OS can preempt your process when you call write to allow more CPU-bound processes to run, before the IO device you're writing to completes the operation.

您可以使用(如果你在* nix机器上开发)唯一较低水平的功能,是使用原始的功能,但即使如此,你的表现会不会是速度远远超过现在。简单地说:IO是昂贵的。

The only lower level function you can use (if you're developing on a *nix machine), is to use the raw write function, but even then your performance will not be that much faster than it is now. Simply put: IO is expensive.

这篇关于发送字节数到标准输出C / C ++最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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