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.0781250 | 0.4062500 |
0 | 0.0781250 | 2.2343750 |
1000 | 0.0781250 | 2.4375000 |
2000 | 0.1093750 | 2.5937500 |
3000 | 0.1093750 | 2.7031250 |
4000 | 0.1250000 | 2.8437500 |
5000 | 0.1406250 | 2.9843750 |
6000 | 0.1250000 | 3.0468750 |
7000 | 0.1406250 | 3.0937500 |
8000 | 0.1406250 | 3.2968750 |
9000 | 0.1562500 | 3.6875000 |
10000 | 0.1875000 | 3.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.3281250 | 0.7968750 |
0 | 0.3437500 | 2.2343750 |
1000 | 0.3750000 | 1.7500000 |
2000 | 0.3593750 | 1.7968750 |
3000 | 0.3593750 | 1.7500000 |
4000 | 0.3437500 | 1.7031250 |
5000 | 0.4062500 | 1.8281250 |
6000 | 0.3437500 | 1.8437500 |
7000 | 0.3750000 | 1.7187500 |
8000 | 0.3593750 | 1.7812500 |
9000 | 0.3593750 | 1.7500000 |
10000 | 0.3593750 | 1.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.2970000 | 1.9840000 |
0 | 0.4220000 | 2.0620000 |
1000 | 0.4220000 | 2.2660000 |
2000 | 0.4210000 | 2.3120000 |
3000 | 0.4370000 | 2.3130000 |
4000 | 0.4220000 | 2.3280000 |
5000 | 0.4380000 | 2.3590000 |
6000 | 0.4380000 | 2.3290000 |
7000 | 0.4380000 | 2.3440000 |
8000 | 0.5000000 | 2.3280000 |
9000 | 0.4370000 | 2.3600000 |
10000 | 0.4530000 | 2.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.0781250 | 0.3281250 | 0.2970000 |
0 | 0.0781250 | 0.3437500 | 0.4220000 |
1000 | 0.0781250 | 0.3750000 | 0.4220000 |
2000 | 0.1093750 | 0.3593750 | 0.4210000 |
3000 | 0.1093750 | 0.3593750 | 0.4370000 |
4000 | 0.1250000 | 0.3437500 | 0.4220000 |
5000 | 0.1406250 | 0.4062500 | 0.4380000 |
6000 | 0.1250000 | 0.3437500 | 0.4380000 |
7000 | 0.1406250 | 0.3750000 | 0.4380000 |
8000 | 0.1406250 | 0.3593750 | 0.5000000 |
9000 | 0.1562500 | 0.3593750 | 0.4370000 |
10000 | 0.1875000 | 0.3593750 | 0.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.4062500 | 0.7968750 | 1.9840000 |
0 | 2.2343750 | 2.2343750 | 2.0620000 |
1000 | 2.4375000 | 1.7500000 | 2.2660000 |
2000 | 2.5937500 | 1.7968750 | 2.3120000 |
3000 | 2.7031250 | 1.7500000 | 2.3130000 |
4000 | 2.8437500 | 1.7031250 | 2.3280000 |
5000 | 2.9843750 | 1.8281250 | 2.3590000 |
6000 | 3.0468750 | 1.8437500 | 2.3290000 |
7000 | 3.0937500 | 1.7187500 | 2.3440000 |
8000 | 3.2968750 | 1.7812500 | 2.3280000 |
9000 | 3.6875000 | 1.7500000 | 2.3600000 |
10000 | 3.3593750 | 1.7500000 | 2.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
|