提取多个字符串的最快方法? [英] Fastest way to extract multiple strings?

查看:80
本文介绍了提取多个字符串的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从流中加载一个巨大的字符串并访问其中写入的一些数据.我使用子字符串访问数据,但速度较慢.有没有一种更快的方法可以将其转换为char数组,或者有一种方法可以直接从流中提取数据?

I have to load a huge string from stream and access some data written in it. I use substring to access the data, but its to slow . Is there a faster way maybe to convert it char array or is there a way to extract the data directly from stream? Thanks in advance.

推荐答案

难怪它很慢,因为您要一遍又一遍地复制原始字符串.这是因为字符串是不可变的.

为什么呢,这是自制的文件格式,所以您需要用一个字符串读取它.

最佳答案取决于您的格式.基本上,您应该在读取文件时将子字符串解析为字符串列表.另一种选择是一次读取所有字符串,然后使用string.Split将其标记化.同样,完全使用什么定界符取决于您的格式.

在所有情况下,您都不要使用整个字符串,也不要在Split之后忘记它.

我不确定哪种方法更快,这取决于令牌的典型长度和总长度.我不认为您应该担心:与您尝试过的相比,时间要快很多.

—SA
No wonder it''s slow, because you copy your original string over and over; and this is because strings are immutable.

Why, what''s is that home-baked file format, so you need to read it in one string.

The best answer depends on your format. Basically, you should parse your sub-strings into list of strings as you read the file. Another option is to read all string at once and tokenize it using string.Split. Again, what delimiters to use totally depends on your format.

In all cases, you don''t use the whole string or forget it after Split.

I''m not sure what way is faster, it depends on typical length of your tokens and total length. I don''t think you should worry: it will be many time faster compared to what you tried.

—SA


我们在说什么?解析从Web请求返回的HTML时,我结合使用了IndexOfSubstring的效果很好.我可以及时处理280K文件.

需要进行哪种解析?数据是否处于可预测的模式?您可以使用Split吗?
How big are we talking about? I use a combination of IndexOf and Substring to great effect when parsing HTML returned from a web request. I can do a 280K file in a timely manner.

What kind of parsing needs to be done? Is the data in a predictable pattern? Can you use Split?


这篇关于提取多个字符串的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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