Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

How can that be? We're in the realm of micro-micro-optimisation here, but I'm working on a log4net appender which writes to Event Tracing for Windows. The setup gives you the runtime configuration of ETW with the easy use of log4net. ETW logging is ultra-efficient, so I want to impact that as little as possible, which is why I'm optimising string comparison.

The appender will let you capture different levels of logging per assembly or per type, and I need to check where the call comes from to identify the correct level. So I can specify INFO level for anything in the namespace Sixeyed.Logging, and DEBUG for the class Sixeyed.Logging.Log.

In that comparison, equality comes first (so a type can be specified more granularly than an assembly), but if not equal I need to check if the type name starts with the specified namespace. StartsWith has to be more efficient than Contains, of course, because we're only comparing the start of the string, not the whole thing. But actually no, for positive matches, StartsWith is consistently 40-50% slower than Contains:

10,000,000 iterations may seem excessive, but not in the context of enterprise logging. For non-matches (e.g. ascertaining that Acme.PetShop.Entities isn't in the Sixeyed.Logging assembly), the two calls are more equally matched. Contains is typically slower, but usually less than 10%:

A very simple optimzation is the fastest - comparing the size of the strings first, then if the test string is longer than the pattern, checking the substring:

var match = (typeName.Length >= lookup.Length);

if (match)

{

     match = typeName.Substring(0, lookup.Length) == lookup;

}

Assert.IsTrue(match);

 That's the fastest for positive and negative checks:

 I'm expecting positive matches to be the dominant scenario, so this optimization saves a second every 10,000,000 logs. That efficient ETW appender for log4net is coming soon...

 

Posted on Friday, March 18, 2011 8:43 AM Logging | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: String comparison: StartsWith() slower than Contains()

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
This is just so confusing to me at first. Thanks for all the helpful tutorials you have provided us. ipad keyboard case
Left by angelo on Aug 07, 2011 8:18 PM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Blog is really informative and entertainng same time. I appriciate the efforts made by Rockets tickets.
Left by IIT EXAM on Aug 22, 2011 2:56 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Thanks for the fix suggestion. I could never write such a script on my own! Twilight. Blog is really informative and entertainng same time. I appriciate the efforts made by Rockets tickets. Browsergames
Left by Gewinnspiele on Nov 21, 2011 9:59 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Thanks a lot for your awesome tutorials,you help me to understand what I did wrong.
Left by friv on Jan 17, 2012 8:31 PM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
I had a problem with String Comparison but your explanation has made it crystal clear.search name with phone number
Left by Roberto Koon on Feb 14, 2012 6:24 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
thanks you for your information
Left by kaos couple murah on Mar 04, 2012 7:00 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
nice info for your posting
Left by dunia pewayangan on Mar 04, 2012 7:03 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
i like it your posting ,, good info
Left by karanganyar online on Mar 04, 2012 7:09 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
I appriciate the efforts made by Rockets tickets.
Left by Florida Immobilien on Mar 10, 2012 8:04 PM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Riviera Maya All-Inclusive resort near Playa del Carmen; Hacienda Tres Rios (★★★★★) boasts a nature park, fine dining restaurants and beachfront luxury suites.
Left by Resort All Inclusive on Mar 26, 2012 11:20 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Rent a luxury holiday villa on the Costa del Sol in Spain with Mijas Villas with on-line availability
Left by Andalucía villas on Mar 26, 2012 11:25 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Export Training Doing business in Nigeria has never been easier, Get ready yourself or your new business for the new global economy with an online Commodities Export Nigeria
Left by export nigeria on Mar 27, 2012 12:51 PM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Dan s Southside Marine of Bloomington and the Twin Cities, Minnesota s Ultimate Marine Dealer A boat dealer for new and used Alumacraft boats, Premier pontoons Stingray boats all powered by Suzuki, Yamaha, Honda outboard motors Volvo Penta I O s Qual
Left by cheap wake boards on Apr 02, 2012 8:40 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Organic skin care line with Gentle, safe and effective! 100 pure natural essential oils, natural skin moisturizer and butters! Our goal is to provide a premium skin care line true, authentic, and safe alternatives to the synthetic, chemical based products on the market today
Left by Essential Oils Organic on Apr 02, 2012 8:41 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
Thank you for sharing the experience along. I’m glad to read your articles.
Left by Flipkart Coupon on Jul 05, 2012 9:17 AM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
I'm expecting positive matches to be the dominant scenario.
Left by Poule d'eau on Sep 27, 2012 2:47 PM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...

Thanks a lot for your great tutorials,your blog is very interesting and contains sets of great info .
download pc games torrents
Left by PlayGameNow on Jun 23, 2015 5:28 PM

# re: String comparison: StartsWith() slower than Contains()
Requesting Gravatar...
I know there will be many difficulties and challenges but I am determined to do it. If it does not succeed then it will be a lesson for me as well
catmario4.com
Left by vdsds on Oct 11, 2017 10:28 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net