Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Comments
Fredrik Bornander 17-May-12 5:38am    
Do not use read to end on a large string.
vino2012 17-May-12 5:41am    
try to use string builder available in system.text;

Then you are doing it wrong!
This code:

C#
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);
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?
 
Share this answer
 
Comments
VJ Reddy 17-May-12 6:14am    
Good answer. 5!
Maciej Los 17-May-12 16:50pm    
Good example! +5!
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900