C ++输入性能 [英] C++ Input Performance

查看:140
本文介绍了C ++输入性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决InterviewStreet上的问题。一段时间后,我确定我实际上花了大量的时间阅读输入。这个特定的问题有很多的输入,所以这使一些有意义。没有意义的是为什么不同的输入方法有不同的表现:

I was trying to solve a problem on InterviewStreet. After some time I determine that I was actually spending the bulk of my time reading the input. This particular question had a lot of input, so that makes some amount of sense. What doesn't make sense is why the varying methods of input had such different performances:

最初我有:

std::string command;
std::cin >> command;

更换速度明显加快:

char command[5];
cin.ignore();
cin.read(command, 5);

重写一切以使用scanf更快速

Rewriting everything to use scanf made it even faster

char command;
scanf("get_%c", &command);

所有人告诉我把阅读输入的时间减少约1/3。

All told I cut the time reading the input down by about a 1/3.

我想知道这些不同方法之间的性能有这样的变化。另外,我想知道为什么使用gprof没有突出我花在I / O的时间,而是似乎指责我的算法的责任。

I'm wondering there is such a variation in performance between these different methods. Additionally, I'm wondering why using gprof didn't highlight the time I was spending in I/O, rather seeming to point the blame to my algorithm.

推荐答案

这些例程有一个很大的变化,因为控制台输入速度几乎从不重要。

There is a big variation in these routines because console input speed almost never matters.

用C写,直接从stdin设备读取并且高效。

And where it does (Unix shell) the code is written in C, reads directly from the stdin device and is efficient.

这篇关于C ++输入性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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