My Name is Kay....

DIY , 먹방 , 개발 , 육아 , 여행 좋아합니다.
AdBlock 사용시 화면이 정상적으로 노출되지 않습니다.
포스팅 관련 문의 및 개발 문의는 Email : wkzkfmxksi@gmail.com

추가 포스팅이 이뤄지지 않는 블로그입니다. 문의는 wkzkfmxksi@gmail.com 으로 연락주세요.
kay
조회 수 11800 추천 수 0 댓글 1
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

결론으로 짧게 정리하면

for문을 사용하고 그 길이를 변수에 저장하고 내부에서 casting 해서 사용하는 것이 foreach 보다 더 빠르다.

경우에 따라 3배정도의 차이가 난다고 합니다....


dataGridView의 출력 속도 때문에 고민하던 중 foreach문에서 느려지는것을 확인하고 찾아본 자료입니다..



처음에는 데브피아에서 자료 (http://www.hoons.net/Board/QACSHAP/Content/33775) 를 찾았는데 돌다보니 아래와 같이 테스트하신 분이 있었네요..


//A comment on a recent post of mine on Stack Overflow suggested that I replace my "for" loop with a more "modern" version using "foreach" and Enumerable.Range. In other words:

// replace this
for (int i = 0; i < 1000; i++)
{
}

// with this
foreach (var i in Enumerable.Range(0, 1000))
{
}
 

//I can't really say that I prefer one over the other, although the second approach does look kinda cool. 
//It'd be even nicer if there were some native support for ranges in C# like there was in Pascal:

// borrowing Pascal's range syntax
foreach (var i in [0..1000])
{
}
//... but I digress. What I wanted to talk about in this post is my findings on the performance difference between a simple "for" loop and a "foreach" over Enumerable.Range.
// I timed two long loops using a Stopwatch:

const int count = 100000000;
var sw = new Stopwatch();

sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
{
}
sw.Stop();
var forTime = sw.ElapsedTicks;

sw.Reset();
sw.Start();
foreach (var i in Enumerable.Range(0, count))
{
}
sw.Stop();
var foreachTime = sw.ElapsedTicks;

Console.WriteLine(forTime);
Console.WriteLine(foreachTime);
Console.WriteLine((float)foreachTime / forTime);
 
/*
The result:

4869915
14286932
2.933713
So the Enumerable.Range approach is three times slower than a simple for loop.

I guess in a real-world situation where the body of the loop is the thing taking the bulk of the time, there wouldn't be that big a difference, but it's interesting that the two approaches differ in performance by so much. Something to be aware of if you're very performance-oriented in your code, anyway.
*/

?
  • ?
    박싱foreach 2017.09.26 13:50
    오래 걸리게 foreach를 썻네요. 박싱언박싱하면 for문이라도 느려집니다.

  1. 화면 캡쳐하기

    Date2013.09.02 CategoryC# Bykay Views4058
    Read More
  2. 프로그래밍 방식으로 버튼의 Click 이벤트 호출

    Date2013.07.23 CategoryC# Bykay Views2730
    Read More
  3. 폼에서 새폼 열고 자신은 완전히 닫기.

    Date2013.07.26 CategoryC# Bykay Views7607
    Read More
  4. 파일 읽기 , 복사 , 삭제 , 쓰기 , 파일 유무

    Date2013.09.27 CategoryVB Bykay Views3950
    Read More
  5. 최근에 사용한 프로젝트 삭제

    Date2013.07.10 CategoryVS Bykay Views2512
    Read More
  6. 웹 사이트 모니터링 프로그램 C#

    Date2015.08.18 CategoryC# Bykay Views2584
    Read More
  7. 엑셀 내보내기 , Exporting SQL To Excel

    Date2013.09.05 CategoryC# Bykay Views4328
    Read More
  8. 엑셀 내보내기 , Exporting DataGridview To Excel

    Date2013.08.08 CategoryC# Bykay Views5117
    Read More
  9. 스레드에서 함수 호출시 매개변수 넘기기

    Date2013.10.29 CategoryC# Bykay Views3209
    Read More
  10. 문자열 검색 " String.Contains() "

    Date2013.09.04 CategoryC# Bykay Views3074
    Read More
  11. [담아온글] For vs Foreach Performance 속도 대결

    Date2013.09.04 CategoryC# Bykay Views11800
    Read More
  12. WindowsForms Application DataGridView

    Date2013.07.05 CategoryC# Bykay Views2646
    Read More
  13. TextBox 에서 줄바꿈 넣기

    Date2013.07.22 CategoryC# Bykay Views10389
    Read More
  14. TextBox KeyEvent 엔터 이벤트 실행하기

    Date2013.07.26 CategoryC# Bykay Views3838
    Read More
  15. SqlCommand 재사용(Reuse) 하기

    Date2013.07.10 CategoryC# Bykay Views3919
    Read More
  16. multiLine TextBox 자동 스크롤시키기

    Date2013.10.29 CategoryC# Bykay Views3388
    Read More
  17. MDI 폼 한번에 모두 닫기

    Date2013.07.25 CategoryC# Bykay Views2824
    Read More
  18. MDI 폼 이동 및 시작 폼 설정하기

    Date2013.07.09 CategoryC# Bykay Views3429
    Read More
  19. IIS에서 .Net 날짜 형식 YYYY-MM-DD

    Date2015.08.24 CategoryVS Bykay Views939
    Read More
  20. DataGridViewComboBoxColumn Change 이벤트 Value 값 가져오기

    Date2013.09.02 CategoryC# Bykay Views5369
    Read More
Board Pagination Prev 1 2 Next
/ 2