最快的搜索在C#中的ASCII文件进行简单的关键字的方法吗? [英] Fastest way to search ASCII files in C# for simple keywords?

查看:203
本文介绍了最快的搜索在C#中的ASCII文件进行简单的关键字的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我搜索的ASCII文件进行简单的关键词是这样的:

  INT SearchInFile(字符串文件,字符串searchString)
{
    INT NUM = 0;

    StreamReader的读卡器= File.OpenText(文件);
    串线= reader.ReadLine();

    而(行!= NULL)
    {
        诠释计数= CountSubstrings(线,searchString);
        如果(计数!= 0)
        {
            NUM + =计数;
        }
        行= reader.ReadLine();
    }

    reader.Close();

    返回NUM;
}
 

这是最快,最有效的记忆方法做这件事?返回计数是可选的,如果它要使搜索的方式的巨大差异,而不是自己。

我用它喜欢的:

  SearchInFile(C:\\的text.txt,酷);
 

解决方案

在c从性能方面最有效的方法非托管$ C $将使用的内存映射,而不是读缓冲区中的文件中的文件。我相信,最好的结果也只能在方式来实现,特别是如果你要扫描的文件可以从远程存储(从服务器上的文件)的文件。

我不知道是相应的.NET 4.0 类的用法将在你的情况完全一样有效。

Right now, I search ASCII files for simple keywords like this:

int SearchInFile (string file, string searchString)
{
    int num = 0;

    StreamReader reader = File.OpenText (file);
    string line = reader.ReadLine();

    while (line != null)
    {
        int count = CountSubstrings(line, searchString);
        if (count != 0)
        {
            num += count;
        }
        line = reader.ReadLine();
    }

    reader.Close();

    return num;
}

Is this the fastest, most memory efficient way to do it? Returning the count is optional if it's going to make a huge difference in the way of searching, but not on its own.

I use it like:

SearchInFile ( "C:\\text.txt", "cool" );

解决方案

In unmanaged code the most effective way from the performance side will be to use Memory-Mapped Files instead of reading the file in buffer. I am sure that the best results can be achieved only in the way, especially if the file which you want to scan could be a file from the remote storage (a file from the server).

I am not sure that the usage of the corresponding .NET 4.0 classes will be in your case exactly the same effective.

这篇关于最快的搜索在C#中的ASCII文件进行简单的关键字的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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