Posts

Showing posts with the label Strings

C# String Performance

In C# I have read a lot of debate over which way is the best way to do String Concatenation.  I have often read never ever use String1 + String2 due to the memory allocations and time constraints.  I decided to run some tests on the various way of doing String manipulation.  The table below shows the iterations, total times, and average times for some common way of doing string addition.  This was just a test of time, there was no memory consumption test.  All tests were doing adding 7 strings together that were stored in variable names.  There was no string a = "a" + "b";  It was all string a  = var1 + var2;  These were creating a single string for the listed iterations using the method(s) below. I was surprised to see the + method came out ahead in all the tests.  String builder came in a close second.  String.Format was extremely slow but allows for localization which is another story. iterations String + average ...

C# Equal vs CompareTo

Image
Out of curiosity I decided to run some speed tests on strings using the Equals and CompareTo methods. I wrote some quick code and came up with some interesting results: This test was run for 10000000 iterations. The Equals methods trounced the CompareTo method. Under the hood Equals stops when the first character that doesn't match is found. CompareTo continues on to the end of the string all the time. However this was a case insensitive search with no culture information. Adding more variables may cloud the water a bit, but that is going to be reserved for future posts. The code used in the test can be seen below. using System; using System.ComponentModel; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Threading; namespace Sandbox { class Program { static void Main( string[] args ) { const int counter = 10000000; int i = 0; int count = ...

Convert string to base64 string

A while back I came across the need to convert regular string text into a base64 string. .Net makes this fairly easy to do. I created a public static class and added two public static methods to encode/decode strings. Now they don't have to be static or public but I tend to make helper/utility classes that way. All of the classes being used are in the System and System.Text namespaces. /// <summary> /// Encodes the string to Base64. /// </summary> /// <param name="valueToEndcode">The value to endcode.</param> /// <returns>A base64 encoded string.</returns> public static string EncodeStringToBase64( string valueToEndcode ) { // encode the string into ascii byte array byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes( valueToEndcode ); // use the byte array to encode to a base64 string string returnValue = Convert.ToBase64String( toEncodeAsBytes ); // drop back the new encoded string value return returnValue; } /...

String.Empty vs ""

Image
So I've always heard you should use String.Empty vs "" the dreaded blank string in C#. So I decided to write a little test program to find out. It simply takes a string variable and assigns it to "" or String.Empty. I do this 1 million times each and time how many milliseconds it takes to do that assignment. My system is a p4 1.5 GHz RAM running .net 2.0. Source code can be sent upon request. The results are surprising and can be seen below. Other then the first run the String.Empty beat the "" by a few milliseconds. Hardly the creaming I've read about. But perhaps if you were doing some severe number crunching you would notice a difference. However this test is hardly definitive and should probably be run several more times and had the numbers aggregated. I've run the app a lot more since yesterday but only added one more screenshot. I may just post the screen UI and then keep track of the results in a spreadsheet or modify the app...