tbiro.com

Forums 
   HackPerfCounter
   RADPass
   SHEdit
   Toys
 
   Contact me
   My PGP Key
 

Checking for empty string performance tests


Search the Internet on .NET performance and you'll be sure to find a large number of String manipulation tips. People use various criteria to give a recommendation such as speed, ease of implementation, coolness and personal preference so you will often find contradictory advice. In my opinion being informed is important so I devised a series of tests that compare various operations. Once you see the numbers your can use your own judgement to form an opinion.

In this articles I will put to test two methods of testing for an empty string:

  • Compare string with String.Empty.
  • Check string length. If the length is 0 then the string is empty.

I will run my test application on .NET v1.1.4322, .NET v2.0.50727 and then on Mono v1.2.2.1 on the same machine with the various frameworks deployed side by side.

Test application:

Each set of tests start with testing the String.Empty constant. After that progressively longer random strings are generated starting from 0 to 10000 and the test is executed for each one.

The operation I am benchmarking is very short so I will run the same test 10,000,000 times to get a clearly visible difference between the various platforms.

Test results:

Please note that the exact test results will depend on your computer performance.

String Length Length test Comparison test
0 (String.Empty)0.07812500.4062500
00.07812502.2343750
10000.07812502.4375000
20000.10937502.5937500
30000.10937502.7031250
40000.12500002.8437500
50000.14062502.9843750
60000.12500003.0468750
70000.14062503.0937500
80000.14062503.2968750
90000.15625003.6875000
100000.18750003.3593750
Table 1 String comparison speed in .NET v1.1.4322 - test is executed 10,000,000 times.
String Length Length test Comparison test
0 (String.Empty)0.32812500.7968750
00.34375002.2343750
10000.37500001.7500000
20000.35937501.7968750
30000.35937501.7500000
40000.34375001.7031250
50000.40625001.8281250
60000.34375001.8437500
70000.37500001.7187500
80000.35937501.7812500
90000.35937501.7500000
100000.35937501.7500000
Table 2 String comparison speed in .NET v2.0.50727 - test is executed 10,000,000 times.
String Length Length test Comparison test
0 (String.Empty)0.29700001.9840000
00.42200002.0620000
10000.42200002.2660000
20000.42100002.3120000
30000.43700002.3130000
40000.42200002.3280000
50000.43800002.3590000
60000.43800002.3290000
70000.43800002.3440000
80000.50000002.3280000
90000.43700002.3600000
100000.45300002.3900000
Table 3 String comparison speed in Mono v1.2.2.1 - test is executed 10,000,000 times.

Here's what it looks like if we plot a graph with the data.


Figure 1 String comparison speed in .NET v1.1.4322 - test is executed 10,000,000 times.


Figure 2 String comparison speed in .NET v2.0.50727 - test is executed 10,000,000 times.


Figure 3 String comparison speed in Mono v1.2.2.1 - test is executed 10,000,000 times.

Cross platform comparisons:

String Length .NET v1.1.4322 .NET v2.0.50727 Mono-1.2.2.1
0 (String.Empty)0.07812500.32812500.2970000
00.07812500.34375000.4220000
10000.07812500.37500000.4220000
20000.10937500.35937500.4210000
30000.10937500.35937500.4370000
40000.12500000.34375000.4220000
50000.14062500.40625000.4380000
60000.12500000.34375000.4380000
70000.14062500.37500000.4380000
80000.14062500.35937500.5000000
90000.15625000.35937500.4370000
100000.18750000.35937500.4530000
Table 4 Cross platform string length test speed - test is executed 10,000,000 times.
String Length .NET v1.1.4322 .NET v2.0.50727 Mono-1.2.2.1
0 (String.Empty)0.40625000.79687501.9840000
02.23437502.23437502.0620000
10002.43750001.75000002.2660000
20002.59375001.79687502.3120000
30002.70312501.75000002.3130000
40002.84375001.70312502.3280000
50002.98437501.82812502.3590000
60003.04687501.84375002.3290000
70003.09375001.71875002.3440000
80003.29687501.78125002.3280000
90003.68750001.75000002.3600000
100003.35937501.75000002.3900000
Table 5 Cross platform string compare test speed - test is executed 10,000,000 times.

Here's what it looks like if we plot a graph with the data.


Figure 4 Cross platform string length test speed - test is executed 10,000,000 times.


Figure 5 Cross platform string compare test speed - test is executed 10,000,000 times.

Conclusions:

Testing string length is faster than comparing with String.Empty on all platforms.

As expected string length is not a significant factor but using String.Empty will yield better performance for a comparison test in both versions of .NET.

Interestingly .NET v1.1.4322 is faster than .NET v2.0.50727 for length tests but it is slower for compare tests. In fact even Mono-1.2.2.1 is faster than .NET v1.1.4322 for the string compare test.

Download code

Comments: tb@tbiro.com






Sitemap generated by Sitemap Manager