Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

Performance Analysis

I ran each of the various conversion methods through some crude Stopwatch performance testing, a run with a random sentence (n=98, 1000 iterations) and a run with a Project Gutenburg text (n=1,189,578, 150 iterations). Here are the results, roughly from fastest to slowest. All measurements are in ticks (10,000 ticks = 1 ms) and all relative notes are compared to the [slowest] StringBuilder implementation.

  • Byte Manipulation
  • Text: 90,465 (12.06X faster)
  • Sentence: 4.3 (14.16X faster)
  • BitConverter
  • Text: 162,098 (6.73X faster)
  • Sentence: 10 (6.09X faster)
  • {SoapHexBinary}.ToString
  • Text: 338,721.1 (3.22X faster)
  • Sentence: 18.2 (3.35X faster)
  • {byte}.ToString("X2") (using foreach)
  • Text: 380,966 (2.86X faster)
  • Sentence: 20.1 (3.03X faster)
  • {byte}.ToString("X2") (using {IEnumerable}.Aggregate, requires System.Linq)
  • Text: 470,690 (2.32X faster)
  • Sentence: 23.1 (2.64X faster)
  • Array.ConvertAll (using string.Join)
  • Text: 952,553 (1.15X faster)
  • Sentence: 31 (1.96X faster)
  • Array.ConvertAll (using string.Concat, requires .NET 4.0)
  • Text: 974,442 (1.12X faster)
  • Sentence: 24.7 (2.47X faster)
  • {StringBuilder}.AppendFormat (using foreach)
  • Text: 999,868 (1.09X faster)
  • Sentence: 53.2 (1.14X faster)
  • {StringBuilder}.AppendFormat (using {IEnumerable}.Aggregate, requires System.Linq)
  • Text: 1,091,130 (1X)
  • Sentence: 60.9 (1X)

NOTE: All tests on AMD Phenom 9750 2.40GHz.

Byte manipulation, while harder to read, is definitely the fastest approach. BitConverter is second, even with the .Replace("-", "") to match its output with the rest. SoapHexBinary took over the third place position when it was added, bumping the two Array.ConvertAll variants.

Recent activity

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.