7
7

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.

csharp > string > 改行の数のカウント > return s.Count( c => c == '\n' ) + 1; > 遅いらしい

Last updated at Posted at 2015-11-19

C#にて改行の数を取得する色々な方法の処理時間。

参考: SO

Here are my timing results for 100000 iterations on a string of ~25k. Lower is faster.

              Time  Factor
Count1   4.8581503     1.4
Count2   4.1406059     1.2
Count3  45.3614124    13.4
Count4   3.3896130     1.0
Count5   5.9304543     1.7

Count3というのが以下。

test.cs
private static int Count3(string s)
{
    return s.Count( c => c == '\n' ) + 1;
}

Count3は遅いが、1行で意味がすぐに分かる上記の書き方はソースリーディングしやすそう。

上記の時間は25k程度の文字で100,000回の処理なので、1000文字程度の処理であれば使っても影響は少ないと考えている。

7
7
2

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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?