코드에 if문이 삽입되면 속도가 느려질까요?

dltkddyd의 이미지

is_partitioned라는 함수템플릿을 보다가 while문과 if문에 대한 의문점이 생겼습니다. is_partitioned라는 함수의 코드는 아래와 같습니다.

template <class InputIterator1, class InputIterator2>
  bool is_partitioned (InputIterator1 first, InputIterator1 last, UnaryPredicate pred)
{
  while (first!=last && pred(*first)) {//A. 여기 블럭에서 의문점
    ++first;
  }
  while (first!=last) {
    if (pred(*first)) return false;
    ++first;
  }
  return true;
}

A블럭을 다음과 같이 고쳐도 될텐데요.

while(first!=last) {
	if(!pred(*first)) {break;}
}

왜 굳이 while 조건부여괄호안에 모든 표현을 담아두려 하는 것인가요?

익명 사용자의 이미지

출구가 하나일때랑 두개일때가 있을때
누가 찾아와서 아무개가 어디로 나갔느냐 물어보면
하나일때는 아무개를 보지못했어도 저기로 나갔어요 하면 되지만
두개 이상이면 아무개를 직접보고 따라다녀야 어디로 나갔는지 알수있으니까요

jick의 이미지

개인마다 다르겠지만, 제가 보기에는 원래 코드가 더 직관적인데요. "first가 last가 아니고 first가 가리키는 곳이 pred를 만족하면 계속 다음 아이템으로 이동해라."

mirheekl의 이미지

고치신 코드에는 first를 증가시키는 코드가 없어보이는데 의도적으로 그리하신 것인지 궁금합니다.

여튼 이부분 제외하고 생각하면 저역시 개인 취향이라는데 동감합니다. 개인적으로는 루프 조건문으로 처리가 가능하면 (너무 길어지지만 않는다면) 가급적 break의 사용을 자제하고 있습니다. 분기가 일어나는 지역이 한곳에 몰려있을수록 읽기가 편해서요. 이역시 개인적인 취향이지요.

--

라스코니의 이미지

느려지거나 하지는 않을 것 같습니다.
동일한 조건 분기문이기 때문에 위치만 다를 뿐이지 동작은 똑같이 할 것 같네요.

이경우 가독성(readability)이 어떤게 좋느냐로 봐야 할것 같네요.

좀 더 복잡한 코드로 봤을 때

  while (first!=last && pred(*first) && pref(*second)) {
    ++first;
  }

로 되어 있다면

  while (first!=last) {
    if(!pred(*first)) break;
 
    if(!pref(*second)) break;
 
    ++first;
  }

로 했을 때 좀 어지러울 것 같네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.