Skip to content

Commit a3152cb

Browse files
committed
Merging changes synced from https://github.com/MicrosoftDocs/visualstudio-docs-pr (branch live)
2 parents 93e1740 + c142bea commit a3152cb

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Performance insights for string concatenations
3+
description: Learn how to improve performance for string concatenations.
4+
ms.date: 7/20/2022
5+
ms.topic: reference
6+
author: mikejo5000
7+
ms.author: mikejo
8+
manager: jmartens
9+
ms.technology: vs-ide-debug
10+
ms.workload:
11+
- multiple
12+
---
13+
14+
# Use StringBuilder for concatenations
15+
16+
This article describes performance insights for string concatenations.
17+
18+
## Cause
19+
20+
Calls to System.String.Concat are a significant proportion of the profiling data. Consider using the <xref:System.Text.StringBuilder> class to construct strings from multiple segments.
21+
22+
## Performance insight description
23+
24+
A <xref:System.String> object is immutable. Therefore, any modification to the string creates a new string object and the garbage collection of the original. This behavior is the same whether you call String.Concat explicitly or use the string concatenation operators such as + or +=. Program performance can decrease if these methods are frequently called, such as when characters are added to a string in a tight loop.
25+
26+
The StringBuilder class is a mutable object, and, unlike System.String, most of the methods on StringBuilder that modify an instance of this class return a reference to that same instance. You can insert characters or append text to a StringBuilder instance, and remove or replace characters in the instance without the need for allocating a new instance and deleting the original instance.
27+
28+
## How to investigate a warning
29+
30+
In the CPU Usage tool, click **View source code** to go to the [call tree](../profiling/cpu-usage.md#BKMK_Call_tree_structure) and source line highlighting view showing where the most amount of time is spent in string concatenations. If the problem is occurring in multiple locations within the application, navigate to the caller/callee view to see each call location.

docs/profiling/toc.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@
3232
href: analyze-database.md
3333
- name: Events Viewer
3434
href: events-viewer.md
35-
- name: CPU usage
36-
href: cpu-usage.md
35+
- name: CPU Usage
36+
items:
37+
- name: CPU usage
38+
href: cpu-usage.md
39+
- name: Performance insights
40+
items:
41+
- name: String concatenation
42+
href: perf-insights-string-concatenation.md
3743
- name: File IO
3844
href: use-file-io.md
3945
- name: Memory usage

0 commit comments

Comments
 (0)