簡體   English   中英

更快的替代Convert.ToBase64String嗎?

[英]Faster alternative to Convert.ToBase64String?

使用.NET Framework; 有沒有更快的替代Convert.ToBase64String(byteArray)

更新:

為了提供更多的背景信息,我正在對發件人不同的電子郵件的文件附件進行編碼,為此,我將在循環內對不少(例如1000個)> 1MB的文件進行編碼。

一個簡化的例子:

for(int i = 0; i < 1000; i++) {
    var file = System.IO.File.ReadAllBytes("c:\\temp\\clientfile" + i + ".pdf");
    System.IO.File.WriteAllText("c:\\emailsendtest\\test" + i + ".tmp", Convert.ToBase64String(file));
}

以下是一些比較這兩種方法(轉換...和密碼學...)的數據。 我進行了200次測試,將100萬個字節寫到一個新的1 MB文件中。 看起來Convert.ToBase64方法比加密方法平均快大約七倍。 測試運行的直方圖如下:

在此處輸入圖片說明

如果有人對驗證我的結果感興趣-這是我的測試代碼:

private static void Test()
{

    Random myRand = new Random();

    List<TimeSpan> convert64Times = new List<TimeSpan>();
    List<TimeSpan> cryptoTimes = new List<TimeSpan>();
    Stopwatch theTimer = new Stopwatch();



    for (int i = 0; i < 200; i++)
    {

        byte[] randBytes = new byte[1000000];
        myRand.NextBytes(randBytes);

        string filePrefix = @"C:\Temp\file";


        // test encode with convert to base 64
        theTimer.Start();
        EncodeWithConvertToBase64(randBytes,filePrefix+i+"convert.txt");
        theTimer.Stop();
        convert64Times.Add(theTimer.Elapsed);
        theTimer.Reset();


        // test encode with crypto
        theTimer.Start();
        EncodeWithCryptoClass(randBytes,filePrefix+i+"crypto.txt");
        theTimer.Stop();
        cryptoTimes.Add(theTimer.Elapsed);
        theTimer.Reset();

    }
}



private static void EncodeWithConvertToBase64(byte[] inputBytes, string targetFile)
{
    string fileString = Convert.ToBase64String(inputBytes);

    using (StreamWriter output = new StreamWriter(targetFile))
    {
        output.Write(fileString);
        output.Close();
    }
}

private static void EncodeWithCryptoClass(byte[] inputBytes, string targetFile)
{

    FileStream outputFileStream =
        new FileStream(targetFile, FileMode.Create, FileAccess.Write);

    // Create a new ToBase64Transform object to convert to base 64.
    ToBase64Transform base64Transform = new ToBase64Transform();

    // Create a new byte array with the size of the output block size.
    byte[] outputBytes = new byte[base64Transform.OutputBlockSize];



    // Verify that multiple blocks can not be transformed.
    if (!base64Transform.CanTransformMultipleBlocks)
    {
        // Initializie the offset size.
        int inputOffset = 0;

        // Iterate through inputBytes transforming by blockSize.
        int inputBlockSize = base64Transform.InputBlockSize;

        while (inputBytes.Length - inputOffset > inputBlockSize)
        {
            base64Transform.TransformBlock(
                inputBytes,
                inputOffset,
                inputBytes.Length - inputOffset,
                outputBytes,
                0);

            inputOffset += base64Transform.InputBlockSize;
            outputFileStream.Write(
                outputBytes,
                0,
                base64Transform.OutputBlockSize);
        }

        // Transform the final block of data.
        outputBytes = base64Transform.TransformFinalBlock(
            inputBytes,
            inputOffset,
            inputBytes.Length - inputOffset);

        outputFileStream.Write(outputBytes, 0, outputBytes.Length);

    }

    // Determine if the current transform can be reused.
    if (!base64Transform.CanReuseTransform)
    {
        // Free up any used resources.
        base64Transform.Clear();
    }

    // Close file streams.

    outputFileStream.Close();
}

假設文件附件以流形式讀取,建議您使用System.Security.Cryptography中的ToBase64Transform類而不是Convert類。

在該頁面上可以找到完整的示例,該示例從輸入文件讀取並寫回編碼的文件。

您還應該看一看JMarsch的示例,在此處找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM