如何加载文字方面的文件到列表,文件中有超过300万线 [英] How to load a file with words into a list where the file has over 3 million lines

查看:108
本文介绍了如何加载文字方面的文件到列表,文件中有超过300万线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能装入3或4万线的文件,在不到1秒(1.000000)?一行包含一个单词。字范围从1长度 - 17(?的确如此)

Is it possible to load a file with 3 or 4 million lines in less than 1 second (1.000000)? One line contains one word. Words range in length from 1 - 17 (does that matter?).

我的code现在是:

List<string> LoadDictionary(string filename)
{
    List<string> wordsDictionary = new List<string>();

    Encoding enc = Encoding.GetEncoding(1250);//I need ę ą ć ł etc.
    using (StreamReader r = new StreamReader(filename, enc))
    {
        string line = "";
        while ((line = r.ReadLine()) != null)
        {
            if (line.Length > 2)
            {
                wordsDictionary.Add(line);
            }
        }
    }

    return wordsDictionary;
}

定时执行的结果:

Results of timed execution:

我怎么能强迫的方法,使其在一半的时间执行?

How can I force the method to make it execute in half the time?

推荐答案

如果你知道你的列表会很大,你应该设置一个很好的起点能力。

If you know that your list will be large, you should set a good starting capacity.

List<string> wordsDictionary = new List<string>( 100000 );

如果你不这样做,该列表将需要不断提高它的能力,这需要一点时间。有可能不会削减下来了一半,但它是一个开始。

If you don't do this, the list will need to keep increasing its capacity which takes a bit of time. Likely won't cut this down by half, but it's a start

这篇关于如何加载文字方面的文件到列表,文件中有超过300万线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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