tellg()的复杂性 [英] complexity for tellg()

查看:88
本文介绍了tellg()的复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在读一个大文件,并且需要为当前文件

位置设置一个标志,以便我可以存储这些位置以供以后直接访问。

然而看起来它是一个非常昂贵的功能!但它的代码是

它应该只返回当前的缓冲区位置,因此应该是一个非常低成本的功能。

To解释,

{

boost :: progress_timer t;

std :: ifstream in(" Y:/ Data / workspaces / tob4f / tob4f.dat");

std :: string line;

while(in){

int pos = in.tellg();

std :: getline(in,line);

}

}

此代码需要0.58秒我的电脑,如果我取消注释

in.tellg(),它需要120.8秒(变化一点点)


可以有人说原因和放大器;可能的锻炼吗?

我有趣的是MS Visual Studio 7.1和视觉提供的std库

studio 7.1

=

解决方案

* toton:




我正在读一个大文件,需要有当前文件的标志

位置,以便我可以存储位置以供以后直接访问。

然而,看起来tellg是一个非常昂贵的功能!但它的代码是

它应该只返回当前的缓冲区位置,因此应该是一个非常低成本的功能。

To解释,

{

boost :: progress_timer t;

std :: ifstream in(" Y:/ Data / workspaces / tob4f / tob4f.dat");

std :: string line;

while(in){

int pos = in.tellg();

std :: getline(in,line);

}

}

此代码需要0.58秒我的电脑,如果我取消注释

in.tellg(),它需要120.8秒(变化一点点)


可以有人说原因和放大器;可能的锻炼吗?

我有趣的是MS Visual Studio 7.1和视觉提供的标准库

studio 7.1



最有可能的原因是CRLF转换为LF,你可以通过(1)以文本模式打开文件和(2)编译
$ b $来指定
b Windows编译器。


一种解决方法是以二进制模式打开文件,并根据需要(或不)处理

换行符。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕东西?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?


toton写道:




我正在读一个大文件,需要有一个当前文件的标志

的位置,以便我可以存储位置供以后直接访问。

然而它看起来像ellg是一个非常昂贵的功能!但它的代码是

它应该只返回当前的缓冲区位置,因此应该是一个非常低成本的功能。

To解释,

{

boost :: progress_timer t;

std :: ifstream in(" Y:/ Data / workspaces / tob4f / tob4f.dat");

std :: string line;

while(in){

int pos = in.tellg();

std :: getline(in,line);

}

}

此代码需要0.58秒我的电脑,如果我取消注释

in.tellg(),它需要120.8秒(变化一点点)


可以有人说原因和放大器;可能的锻炼?

我有趣的是MS Visual Studio 7.1和视觉提供的std库

studio 7.1



原因是tellg执行当前位置的搜索。这个

会刷新输入缓冲区,从而大大减慢你的程序。


通过定义看起来是streambuf(所有人都使用

streams)是这样找到当前位置的唯一方法是

执行寻找当前位置。


john


>


通过定义看起来是streambuf(所有

流使用)是这样找到当前位置的唯一方法是为了
执行寻找当前位置。



让我再试一次


看起来好像streambuf的定义(所有

流使用)是这样的,找到当前位置的唯一方法是

执行寻找当前位置。


john


Hi,
I am reading a big file , and need to have a flag for current file
position so that I can store the positions for later direct access.
However it looks tellg is a very costly function ! But it''s code says
it should just return the current buffer position , thus should be a
very low cost function.
To explain,
{
boost::progress_timer t;
std::ifstream in("Y:/Data/workspaces/tob4f/tob4f.dat");
std::string line;
while(in){
int pos = in.tellg();
std::getline(in,line);
}
}
This code takes 0.58 sec in my computer, while if I uncomment the line
in.tellg() , it takes 120.8 sec (varies a little )

can anyone say the reason & the possible workout ?
I amusing MS Visual Studio 7.1 and the std library provided by visual
studio 7.1

解决方案

* toton:

Hi,
I am reading a big file , and need to have a flag for current file
position so that I can store the positions for later direct access.
However it looks tellg is a very costly function ! But it''s code says
it should just return the current buffer position , thus should be a
very low cost function.
To explain,
{
boost::progress_timer t;
std::ifstream in("Y:/Data/workspaces/tob4f/tob4f.dat");
std::string line;
while(in){
int pos = in.tellg();
std::getline(in,line);
}
}
This code takes 0.58 sec in my computer, while if I uncomment the line
in.tellg() , it takes 120.8 sec (varies a little )

can anyone say the reason & the possible workout ?
I amusing MS Visual Studio 7.1 and the std library provided by visual
studio 7.1

Most likely the cause is conversion of CRLF to LF, which you''ve
specified by (1) opening the file in text mode and (2) compiling with a
Windows compiler.

One cure could then be to open the file in binary mode, and handle
newlines as appropriate (or not).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


toton wrote:

Hi,
I am reading a big file , and need to have a flag for current file
position so that I can store the positions for later direct access.
However it looks tellg is a very costly function ! But it''s code says
it should just return the current buffer position , thus should be a
very low cost function.
To explain,
{
boost::progress_timer t;
std::ifstream in("Y:/Data/workspaces/tob4f/tob4f.dat");
std::string line;
while(in){
int pos = in.tellg();
std::getline(in,line);
}
}
This code takes 0.58 sec in my computer, while if I uncomment the line
in.tellg() , it takes 120.8 sec (varies a little )

can anyone say the reason & the possible workout ?
I amusing MS Visual Studio 7.1 and the std library provided by visual
studio 7.1

The reason is that tellg performs a seek to the current position. This
flushes the input buffer so dramatically slowing down your program.

Looks as through the defintion is streambuf (which is used by all
streams) is such that the only way to find the current position is to
perform a seek to the current position.

john


>

Looks as through the defintion is streambuf (which is used by all
streams) is such that the only way to find the current position is to
perform a seek to the current position.

Let me try that again

Looks as though the definition of streambuf (which is used by all
streams) is such that the only way to find the current position is to
perform a seek to the current position.

john


这篇关于tellg()的复杂性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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