배경일리노이 코드를 비교하여 어느 기술이 더 빠르고 더 좋은지 판단 할 수 있습니까?
This question 나에 대해 생각해 보았습니다. 최근에 제가 linq pad의 일리노이 기능을보고 있었기 때문에 동일한 문제에 대한 두 가지 접근 방식의 일리노이 코드를 비교해 가장 잘 판단했습니다. 제조
var arr = new string[] { "1", "2", "3", "4" };
var result = Array.ConvertAll(arr, s => Int32.Parse(s));
:
IL_0001: ldc.i4.4
IL_0002: newarr System.String
IL_0007: stloc.2
IL_0008: ldloc.2
IL_0009: ldc.i4.0
IL_000A: ldstr "1"
IL_000F: stelem.ref
IL_0010: ldloc.2
IL_0011: ldc.i4.1
IL_0012: ldstr "2"
IL_0017: stelem.ref
IL_0018: ldloc.2
IL_0019: ldc.i4.2
IL_001A: ldstr "3"
IL_001F: stelem.ref
IL_0020: ldloc.2
IL_0021: ldc.i4.3
IL_0022: ldstr "4"
IL_0027: stelem.ref
IL_0028: ldloc.2
IL_0029: stloc.0
IL_002A: ldloc.0
IL_002B: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0030: brtrue.s IL_0045
IL_0032: ldnull
IL_0033: ldftn b__0
IL_0039: newobj System.Converter<System.String,System.Int32>..ctor
IL_003E: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0043: br.s IL_0045
IL_0045: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_004A: call System.Array.ConvertAll
IL_004F: stloc.1
b__0:
IL_0000: ldarg.0
IL_0001: call System.Int32.Parse
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000A: ret
다른 않음 :
상기 연결 질문 사용배열 변환에 대해서는, 두 답변은 IL 코드 생성
var arr = new string[] { "1", "2", "3", "4" };
var result = arr.Select(s => int.Parse(s)).ToArray();
제작 :
IL_0001: ldc.i4.4
IL_0002: newarr System.String
IL_0007: stloc.2
IL_0008: ldloc.2
IL_0009: ldc.i4.0
IL_000A: ldstr "1"
IL_000F: stelem.ref
IL_0010: ldloc.2
IL_0011: ldc.i4.1
IL_0012: ldstr "2"
IL_0017: stelem.ref
IL_0018: ldloc.2
IL_0019: ldc.i4.2
IL_001A: ldstr "3"
IL_001F: stelem.ref
IL_0020: ldloc.2
IL_0021: ldc.i4.3
IL_0022: ldstr "4"
IL_0027: stelem.ref
IL_0028: ldloc.2
IL_0029: stloc.0
IL_002A: ldloc.0
IL_002B: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0030: brtrue.s IL_0045
IL_0032: ldnull
IL_0033: ldftn b__0
IL_0039: newobj System.Func<System.String,System.Int32>..ctor
IL_003E: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0043: br.s IL_0045
IL_0045: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_004A: call System.Linq.Enumerable.Select
IL_004F: call System.Linq.Enumerable.ToArray
IL_0054: stloc.1
b__0:
IL_0000: ldarg.0
IL_0001: call System.Int32.Parse
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000A: ret
이 보면, 내가 말할 수있는 것은 후자의 옵션
- 가 작성하는 첫번째 대답은
- 하지 않는 경우
- 는 LINQ를 사용하여 1 개 개의 여분 줄을 취한다는 것이다 다르게 IL_0039을 통해 지능의.
질문이 구체적인 예를 들어
- , 내 가정이 정확한지?
- 일반적으로 IL 코드를 통해 두 솔루션을 비교하는 방법은 무엇입니까?
- 일반적으로 IL LOC가 적은 솔루션은 메모리가 더 빨리 또는 더 적게 사용된다는 의미입니까?
- 제목에서 알 수 있듯이 일리노이 코드를 비교하여 어느 기술이 더 빠르고 더 좋은지 판단 할 수 있습니까?
FWIW, 저는 드문 경우지만 모든 토론이 직장에서 개발자들 사이에서 발생하는 경우가 종종 있습니다. 누군가는 "오 이런식이 더 효율적"이라고 말할 것이고 IL 코드를 확인하기 위해 linqpad에 던져 보겠습니다. 또한 FWIW, 나는 거의 항상 그것을 효율적/빠른 접근법을 얻기 전에 작동시키는 법을 준수합니다. 나는이 구체적인 예를 들어 :
안녕, 알렌, 대단히 감사합니다! – abatishchev