2
2

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.

IEnumerableで要素数がn個だったらというチェック

Last updated at Posted at 2017-09-07

思いついた方法が二つあって、計算量がややこしいのでどっちが効率いいのか考えてみました。

var count = list.Count();
if(count == n)
{
//n=1の時は list.Any() && !list.Skip(n).Any()でもOK
if(list.Skip(n - 1).Any() && !list.Skip(n).Any())
{

IEnumerable#Count()は全要素を列挙してカウントします。計算量はlistの要素数が影響します(当然多いほど遅い)。Any()は最初の要素だけ列挙してBreakしてると思われるのでlistの要素数は影響しません。Skip()は引数に与えられた数だけ列挙してると思われるので引数のnが計算量に影響します。

どちらが早いかはlistの総カウントとnの両方に依存することになります。結構ややこしいですね・・・いちいちベンチ取るしかないかな。LISTのカウントとnを入れるとベンチを取ってくれるWEBツールがあるといいですね。今、手元に公開できるC#の動くサーバーが無いのでまたの機会にします。

ベンチ

試しにベンチを取って見ました。(それぞれ1000000回実行した秒数です)

要素数10 1<=n<=9

COUNT 0.08688519sec n: 1
SKIP  0.13536301sec n: 1

COUNT 0.08437488sec n: 2
SKIP  0.13601078sec n: 2

COUNT 0.08058210sec n: 3
SKIP  0.13479600sec n: 3

COUNT 0.07993681sec n: 4
SKIP  0.15452508sec n: 4

COUNT 0.08438793sec n: 5
SKIP  0.14931089sec n: 5

COUNT 0.08125596sec n: 6
SKIP  0.16214308sec n: 6

COUNT 0.07907924sec n: 7
SKIP  0.19363627sec n: 7

COUNT 0.08123375sec n: 8
SKIP  0.17400098sec n: 8

COUNT 0.08016390sec n: 9
SKIP  0.18064998sec n: 9

要素数30 1<=n<=29

COUNT 0.15943179sec n: 1
SKIP  0.13440001sec n: 1

COUNT 0.15907705sec n: 2
SKIP  0.15324895sec n: 2

COUNT 0.15385581sec n: 3
SKIP  0.15094105sec n: 3

COUNT 0.15155003sec n: 4
SKIP  0.15132929sec n: 4

COUNT 0.15053695sec n: 5
SKIP  0.15245803sec n: 5

COUNT 0.16269881sec n: 6
SKIP  0.15841695sec n: 6

COUNT 0.14897202sec n: 7
SKIP  0.16994092sec n: 7

COUNT 0.15018292sec n: 8
SKIP  0.17811006sec n: 8

COUNT 0.14989201sec n: 9
SKIP  0.17823207sec n: 9

COUNT 0.14884896sec n: 10
SKIP  0.18676723sec n: 10

COUNT 0.14831721sec n: 11
SKIP  0.19475478sec n: 11

COUNT 0.15004187sec n: 12
SKIP  0.20300291sec n: 12

COUNT 0.15208813sec n: 13
SKIP  0.20805102sec n: 13

COUNT 0.14660101sec n: 14
SKIP  0.20904329sec n: 14

COUNT 0.16910486sec n: 15
SKIP  0.22802693sec n: 15

COUNT 0.14786586sec n: 16
SKIP  0.23907205sec n: 16

COUNT 0.14849105sec n: 17
SKIP  0.26083892sec n: 17

COUNT 0.15115016sec n: 18
SKIP  0.26024299sec n: 18

COUNT 0.14975131sec n: 19
SKIP  0.25071591sec n: 19

COUNT 0.15001296sec n: 20
SKIP  0.27903481sec n: 20

COUNT 0.15756996sec n: 21
SKIP  0.26791177sec n: 21

COUNT 0.14960110sec n: 22
SKIP  0.27541517sec n: 22

COUNT 0.15017587sec n: 23
SKIP  0.28378777sec n: 23

COUNT 0.14580902sec n: 24
SKIP  0.28498104sec n: 24

COUNT 0.15334310sec n: 25
SKIP  0.28784184sec n: 25

COUNT 0.15154086sec n: 26
SKIP  0.30863619sec n: 26

COUNT 0.14755202sec n: 27
SKIP  0.31374107sec n: 27

COUNT 0.14913599sec n: 28
SKIP  0.31061932sec n: 28

COUNT 0.14858802sec n: 29
SKIP  0.32072894sec n: 29

要素数100 1<=n<=50

COUNT 0.35548280sec n: 1
SKIP  0.12780990sec n: 1

COUNT 0.34627413sec n: 2
SKIP  0.13351811sec n: 2

COUNT 0.36455394sec n: 3
SKIP  0.14014313sec n: 3

COUNT 0.35904884sec n: 4
SKIP  0.14739194sec n: 4

COUNT 0.34664227sec n: 5
SKIP  0.14899988sec n: 5

COUNT 0.34295386sec n: 6
SKIP  0.15708193sec n: 6

COUNT 0.34352898sec n: 7
SKIP  0.16527223sec n: 7

COUNT 0.34516796sec n: 8
SKIP  0.17206404sec n: 8

COUNT 0.34318306sec n: 9
SKIP  0.18129598sec n: 9

COUNT 0.34371693sec n: 10
SKIP  0.19026981sec n: 10

COUNT 0.34110578sec n: 11
SKIP  0.19495013sec n: 11

COUNT 0.34406391sec n: 12
SKIP  0.19804296sec n: 12

COUNT 0.34176800sec n: 13
SKIP  0.20717017sec n: 13

COUNT 0.34706611sec n: 14
SKIP  0.22112405sec n: 14

COUNT 0.35393021sec n: 15
SKIP  0.22262092sec n: 15

COUNT 0.34526211sec n: 16
SKIP  0.23557617sec n: 16

COUNT 0.34390805sec n: 17
SKIP  0.24751694sec n: 17

COUNT 0.34824175sec n: 18
SKIP  0.25229600sec n: 18

COUNT 0.34132899sec n: 19
SKIP  0.25072190sec n: 19

COUNT 0.34307587sec n: 20
SKIP  0.26490709sec n: 20

COUNT 0.34396623sec n: 21
SKIP  0.26642019sec n: 21

COUNT 0.34332623sec n: 22
SKIP  0.26910997sec n: 22

COUNT 0.34318800sec n: 23
SKIP  0.27709716sec n: 23

COUNT 0.35033772sec n: 24
SKIP  0.28053697sec n: 24

COUNT 0.34508086sec n: 25
SKIP  0.29886615sec n: 25

COUNT 0.34138400sec n: 26
SKIP  0.31127590sec n: 26

COUNT 0.34214707sec n: 27
SKIP  0.30926315sec n: 27

COUNT 0.34338794sec n: 28
SKIP  0.31609410sec n: 28

COUNT 0.33892412sec n: 29
SKIP  0.31961889sec n: 29

COUNT 0.34913494sec n: 30
SKIP  0.32460105sec n: 30

COUNT 0.35027319sec n: 31
SKIP  0.34174014sec n: 31

COUNT 0.34157406sec n: 32
SKIP  0.33185586sec n: 32

COUNT 0.34085507sec n: 33
SKIP  0.35337589sec n: 33

COUNT 0.36562802sec n: 34
SKIP  0.35539887sec n: 34

COUNT 0.33971117sec n: 35
SKIP  0.34875798sec n: 35

COUNT 0.34358399sec n: 36
SKIP  0.35080812sec n: 36

COUNT 0.34050809sec n: 37
SKIP  0.35420702sec n: 37

COUNT 0.34160826sec n: 38
SKIP  0.36413503sec n: 38

COUNT 0.34033919sec n: 39
SKIP  0.36870499sec n: 39

COUNT 0.34383471sec n: 40
SKIP  0.37407116sec n: 40

COUNT 0.34056098sec n: 41
SKIP  0.38126107sec n: 41

COUNT 0.35250598sec n: 42
SKIP  0.39190103sec n: 42

COUNT 0.34235793sec n: 43
SKIP  0.38751408sec n: 43

COUNT 0.34066994sec n: 44
SKIP  0.40268203sec n: 44

COUNT 0.34582525sec n: 45
SKIP  0.41063184sec n: 45

COUNT 0.33965299sec n: 46
SKIP  0.40651395sec n: 46

COUNT 0.34476880sec n: 47
SKIP  0.41450114sec n: 47

COUNT 0.34590494sec n: 48
SKIP  0.41863314sec n: 48

COUNT 0.34418027sec n: 49
SKIP  0.41837573sec n: 49

COUNT 0.34255399sec n: 50
SKIP  0.43121814sec n: 50
2
2
6

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?