C언어, 함수 반환타입을 int형 또는 unsigned int형으로 한다면 더 좋은 성능이 나올수 있나요?

gurumong의 이미지

함수 반환타입을 int형 또는 unsigned int형으로 한다면 더 좋은 성능이 나올수 있을까요?

특정 비트값을 조사해서 0 또는 1을 반환하는 함수를 만들려고하는데요
반환타입을 가장 작은 타입인 unsign char가 아닌 넉넉한 int형 또는 unsigned int으로 하는게 어떤 이점이 있을까요?

시스템에서 워드형인 int형으로 다룰때가 가장 좋은 성능을 보인다고 들은게 있어서요
사실 반환하는 값의 범위만을 따진다면 가장 작은 무부호 char도 넘치는데요;

neogeo의 이미지

char 로 넘기나 int 로 넘기나 int 가 기본적인 cpu 의 data 다루는 사이즈와 같다면 별 성능 차이가 없습니다.

( 예를들어 x86 같은경우 32bit의 register 에 담아서 function call 을 하기위해 stack push 를 하던, memeory 에 담아서 push 를 하던 어차피 32bit 단위로 움직입니다. 최종적인 녀석이 pop 할때도 어차피 32bit 단위로 주소가 align 되서 움직이지요. sizeof (int) 가 4라는 가정하에서 입니다. )

리턴값을 받는 변수나 평가식의 type 이 오히려 중요합니다. type 이 다르면 casting 이 발생할테니 아무래도 성능에 조금이라도 영향을 줄 수 있겠지요.

Neogeo - Future is Now.

Neogeo - Future is Now.

라스코니의 이미지

윗분 말씀처럼 기본 32 bit 단위로 움직이기 때문에 32 bit 단위로 받는게 제일 빠른 길 입니다. 이것을 char, bool 등으로 해석하는 것도 일종의 casting 이기 때문에 프로그램의 입장에서 보면 추가적인 연산이 필요하게 됩니다.

하지만,,

이것을 통해서 얻을 수 있는 성능의 향상은 거의 없다가 정답입니다. 초당 수십만번 실행되는 함수가 아니라면 차라리 쉽게 code reading 을 할 수 있는 bool type 으로 처리하는게 맞습니다. 아니면 별도의 enumerate type bit_value = { unset = 0, set = 1 }; 등으로 처리하는 것이 더 좋겠네요.

poplinux의 이미지

cpu에 따라 틀립니다.

그리고 체감 속도 차이는 그다지..

========================
조직 : E.L.D(Embedded Linux Developer/Designer)
블로그 : poplinux@tistory.com
카페 : cafe.naver.com/poplinux

임베디드 리눅스 관련 프리렌서 지향

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.