AboutSQL Server, Analytics, .Net, Machine Learning, R, Python Archives
About Me
Mitch Wheat has been working as a professional programmer since 1984, graduating with a honours degree in Mathematics from Warwick University, UK in 1986. He moved to Perth in 1995, having worked in software houses in London and Rotterdam. He has worked in the areas of mining, electronics, research, defence, financial, GIS, telecommunications, engineering, and information management. Mitch has worked mainly with Microsoft technologies (since Windows version 3.0) but has also used UNIX. He holds the following Microsoft certifications: MCPD (Web and Windows) using C# and SQL Server MCITP (Admin and Developer). His preferred development environment is C#, .Net Framework and SQL Server. Mitch has worked as an independent consultant for the last 10 years, and is currently involved with helping teams improve their Software Development Life Cycle. His areas of special interest lie in performance tuning |
Sunday, July 22, 2007StringBuilder.AppendFormat() versus String.Format()If you are familiar with .NET then you will probably already know that for efficiency, you should use a StringBuilder rather than concatenating strings in a loop. I recently wrote some code that looked similar to this: StringBuilder sb = new StringBuilder(); ... sb.Append(String.Format("{0} {1}", areaCode, phoneNumber)); where this code was called many times in a loop (not instantiation of the StringBuilder, obviously). It turns out there is a better way of doing it, using one of the StringBuilder class’s methods: sb.AppendFormat("{0} {1}", areaCode, phoneNumber); As Dustin Campbell mentions here, the reason this has slightly better performance is because internally, String.Format() actually creates a StringBuilder and calls StringBuilder.AppendFormat() |
ContactMSN, Email: mitch døt wheat at gmail.com LinksFavorites
Blogs |