程序输出可以通过程序本身重定向到管道吗?
嗯,这是关于比赛的节目。
我正在提交一个程序&发现我的指标在总执行速度方面比得分最高的人要慢得多。所有其他的(页面错误、内存......)都是相似的。我发现当我在没有 printf (或 write)的情况下运行我的程序时,我的总执行速度(在我自己的电脑上测量)似乎相似。
竞赛通过将输出(我想是用管道)重定向到文件和文件中来评估输出。将其 MD5 与他们的相匹配...
我的问题是,C 中是否有某种东西不会写入输出流,但管道仍然会获取其输入。或者也许我什至错误地提出了这个问题。但无论如何,我都陷入了困境。
我一直在努力优化算法。顺便说一句,他们接受许多人试图优化的 makefile。对我来说,这两个优化标志都不起作用。我也不知道对此还能做什么......
Well this is regarding a program for a competition.
I was submitting a program & finding my metrics to be relatively way slower than the top scorers in terms of total execution speed. All others (page faults, memory...) were similar. I found that when I ran through my program without the printf (or write) my total execution speed (as measured in my own pc) seemed to be similar.
The competition evaluates the output by redirecting the output (with a pipe, i suppose) into a file & matching its MD5 with theirs....
My question is, Is there by any means something in C, that doesn't write to the output stream but still the pipe gets its input. Or perhaps I am even framing the question wrong. But either way, I am in a fix.
I have been beating my head off with optimizing the algorithm. BTW they accept makefile where many have tried to optimize. For me neither of the optimization flags have worked. I don't know what else can be done about that too...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要编写一个将其输出写入文件的程序,您只需:
int fd = fopen("/file/path", O_WRONLY);
打开文件(您可能需要检查参数,已经很久没有做过C编程了)然后write(fd, ...);
或者fprintf(fd, ...);< /code>
If you need to make a program that writes its output to a file, you just need to:
int fd = fopen("/file/path", O_WRONLY);
(you may need to check the parameters, it's been a long time since I've done C programming) and thenwrite(fd, ...);
orfprintf(fd, ...);
dup2()
to duplicate the file descriptor to the file descriptor number 1 (i.e. standard output).您可以在管道 fd 上尝试 fprintf 。
You may try fprintf on the pipe fd.