17
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#ใงๆ€ง่ƒฝใฎใ„ใ„ๆนฏๅฉ†ๅฉ†

17
Last updated at Posted at 2020-11-09

ๅๅ‰ใ‚’ๅ‰Šใฃใฆใ€ŒไปŠๆ—ฅใ‹ใ‚‰ใŠๅ‰ใฎๅๅ‰ใฏ๏ฝžใ ใ€ใ‚’็”Ÿๆˆใ™ใ‚‹C#ใฎใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆคœ่จŽใ—ใฆใ„ใ‚‹่จ˜ไบ‹ใŒใ‚ใ‚‹ใ€‚

C# ใงๆนฏๅฉ†ๅฉ†ใ‚’ๅฎŸ่ฃ…ใ—ใฆใฟใ‚‹ใ€Œๆ€ง่ƒฝใฎใ„ใ„ๆนฏๅฉ†ๅฉ†ใ‚’็›ฎๆŒ‡ใ—ใฆใ€

ใ“ใ‚Œใซใ‚‚ใ†ไธ€ใคใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๅŠ ใˆใฆใฟใŸใ„ใ€‚

ๆ–‡ๅญ—ๅˆ—ใ‚’ใŸใใ•ใ‚“้€ฃ็ตใ™ใ‚‹ๅ ดๅˆใฏใ€ไธ€่ˆฌใซStringBuilderใ‚’ไฝฟใ†ใฎใŒใ‚ˆใ„ใจใ•ใ‚Œใฆใ„ใ‚‹ใŒใ€ไธ€ใคใฎๅผใง+ใงใคใชใใ“ใจใŒใงใใ‚‹ใชใ‚‰ใใ†ใ—ใŸใปใ†ใŒ้€Ÿใ„ใ€‚ใใ†ใ™ใ‚‹ใจใ€+ใงใคใชใ„ใ ๆ–‡ๅญ—ๅˆ—ใ‚’ใ™ในใฆ้…ๅˆ—ใซๆ ผ็ดใ—ใฆString.Concatใ‚’ไธ€ๅ›žๅ‘ผใถใ‚ณใƒผใƒ‰ใŒๅใ‹ใ‚Œใ‚‹ใ‹ใ‚‰ใ ใ€‚

[Benchmark]
public string ConcatWorkstyle()
{
    var newName = Name[1];
    return "ๅฅ‘็ด„ๆ›ธใ ใ‚ˆใ€‚ใใ“ใซๅๅ‰ใ‚’ๆ›ธใใชใ€‚\r\nใƒ•ใƒณใ€‚" + Name + "ใจใ„ใ†ใฎใ‹ใ„ใ€‚่ด…ๆฒขใชๅใ ใญใ‡ใ€‚\r\nไปŠใ‹ใ‚‰ใŠๅ‰ใฎๅๅ‰ใฏ" +
           newName + "ใ ใ€‚ใ„ใ„ใ‹ใ„ใ€" + newName + "ใ ใ‚ˆใ€‚ๅˆ†ใ‹ใฃใŸใ‚‰่ฟ”ไบ‹ใ‚’ใ™ใ‚‹ใ‚“ใ ใ€" + newName + "!!";
}

ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใฏ.NET Core 3.1 on AMD Ryzen 7 3800Xใงใ€‚ใใ‚Œใงใ‚‚ZStringใŒๆœ€้€Ÿใชใฎใงใ€ไฝฟใˆใ‚‹ๅ ดๅˆใฏZStringใ‚’ไฝฟใ†ในใใ‹ใจใ€‚

|               Method |      Mean |    Error |   StdDev |
|--------------------- |----------:|---------:|---------:|
| TraditionalWorkstyle | 455.32 ns | 6.832 ns | 6.391 ns |
|         NewWorkstyle | 194.72 ns | 3.654 ns | 4.751 ns |
|     AnotherWorkstyle | 284.55 ns | 5.091 ns | 4.762 ns |
|     ZStringWorkstyle |  97.90 ns | 1.102 ns | 1.031 ns |
|      ConcatWorkstyle | 101.58 ns | 1.525 ns | 1.427 ns |

17
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?