使用大弦 [英] working with large string

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

问题描述

我正在使用c#处理大型字符串,例如我的字符串长度为2.000.000个字符.我必须对该字符串进行加密.我必须将txt文件保存在硬盘上.我尝试对XOR进行加密以获得最快的基本文本加密,但加密时间仍然太长(使用2.13 ghz duo cpu和3gb ram需要1个小时).还要保存文件(我正在使用StreamWriter Write方法)并从文件中读取文件(我正在使用StreamReader ReadToEnd方法)花费的时间太长.

您对大字符串有何建议?

I am working with large string with c#.For example my string length 2.000.000 character.I have to crypto this string.I have to save as txt file on harddisk.I try to crypto XOR for the fastest and basic text encryption but still takes too long encryption(It takes 1 hour with 2.13 ghz duo cpu and 3gb ram ).Also save the file(I am using StreamWriter Write method) and read from the file (I am using StreamReader ReadToEnd method) takes too long.

What is your advice for large string?

推荐答案

然后您做错了!
这段代码:

Then you are doing it wrong!
This code:

string s1 = @"D:\Temp\MyLargeTextFile.txt";
string s2 = @"D:\Temp\mlt.txt";
Stopwatch s = new Stopwatch();
s.Start();
byte[] bytes = File.ReadAllBytes(s1);
for (int i = 0; i < bytes.Length; i++)
    {
    bytes[i] ^= 0x55;
    }
File.WriteAllBytes(s2, bytes);
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);

在E6700/4GB ram上的调试器中,在一个8,651,780字节的文本文件上花费74毫秒.

那么,您这样做的速度要慢得多吗?

takes 74 milliseconds, on my E6700 / 4GB ram, in the debugger, on a 8,651,780 byte text file.

So what are you doing that is so much slower?


如果您确实需要使用这么大的字符串,那么我建议您考虑改用C或C ++,并使用Windows.直接使用API​​.虽然.NET提供了许多出色的功能,但有时如果您需要访问最后的性能下降,则必须走出舒适区.

如果您必须像这样用C#加密/解密数据,我建议您需要类似
If you really need to work with a string this large, then I would advise that you consider using C or C++ instead and use the Windows API directly. While .NET provides many wonderful features, sometimes you have to step outside that comfort zone if you need to access the very last drop of performance.

If you must enxrypt/decrypt your data in C# like this, I''d suggest that you need something like this[^] article.

Without seeing your code, it''s hard to say what exactly is going wrong, but I''d hazard a guess that you are using a lot of string concatenation in there. If you are, don''t use a string, use a StringBuilder instead.


这篇关于使用大弦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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