Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:19:32+00:00 2026-06-05T03:19:32+00:00

I wonder if it’s a good idea to keep using int (which is 32

  • 0

I wonder if it’s a good idea to keep using int (which is 32 bit on both x86 and x86_64) on 64 bit programs for variables that have nothing special and do not really need to span up to 2^64, like iteration counters, or if it’s better to use size_t which matches the word size of the CPU.

For sure if you keep using int you save half of the memory, and that could mean something speaking about CPU cache, but I don’t know then if on 64 bit machine every 32 bit number has to be extended to 64 bit before any use.

EDIT:
I’ve ran some test with a program of mine (see the self answer, I still keep janneb’s as accepted though because it is good). It turns out that there is a significant performance improvement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:19:35+00:00Added an answer on June 5, 2026 at 3:19 am

    For array indices and pointer arithmetic, types which are of the same size as a pointer (typically, size_t and ptrdiff_t) can be better, as they avoid the need to zero or sign extend the register. Consider

    
    float onei(float *a, int n)
    {
      return a[n];
    }
    
    float oneu(float *a, unsigned n)
    {
      return a[n];
    }
    
    float onep(float *a, ptrdiff_t n)
    {
      return a[n];
    }
    
    float ones(float *a, size_t n)
    {
      return a[n];
    }
    

    With GCC 4.4 -O2 on x86_64 the following asm is generated:

    
        .p2align 4,,15
    .globl onei
        .type   onei, @function
    onei:
    .LFB3:
        .cfi_startproc
        movslq  %esi,%rsi
        movss   (%rdi,%rsi,4), %xmm0
        ret
        .cfi_endproc
    .LFE3:
        .size   onei, .-onei
        .p2align 4,,15
    .globl oneu
        .type   oneu, @function
    oneu:
    .LFB4:
        .cfi_startproc
        mov %esi, %esi
        movss   (%rdi,%rsi,4), %xmm0
        ret
        .cfi_endproc
    .LFE4:
        .size   oneu, .-oneu
        .p2align 4,,15
    .globl onep
        .type   onep, @function
    onep:
    .LFB5:
        .cfi_startproc
        movss   (%rdi,%rsi,4), %xmm0
        ret
        .cfi_endproc
    .LFE5:
        .size   onep, .-onep
        .p2align 4,,15
    .globl ones
        .type   ones, @function
    ones:
    .LFB6:
        .cfi_startproc
        movss   (%rdi,%rsi,4), %xmm0
        ret
        .cfi_endproc
    .LFE6:
        .size   ones, .-ones
    

    As can be seen, the versions with the int and unsigned int index (onei and oneu) requires an extra instruction (movslq/mov) to sign/zero extend the register.

    As was mentioned in a comment, the downside is that encoding a 64-bit register takes more space than the 32-bit part, bloating the code size. Secondly, ptrdiff_t/size_t variables need more memory than the equivalent int; if you have such arrays it can certainly affect performance much more than the relatively small benefit of avoiding the zero/sign extension. If unsure, profile!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if I can execute some programs for different cmd's using .bat file.
I wonder if there is a possibility to make the dynamic type for variables
I wonder whether someone may be able to help me please. I'm using the
I wonder how to mark a specific test in eunit in a way that
I wonder, if someone could help me... Is there a LINQ query that will
I wonder that, Ruby programming language is ...(easy/difficult) to learn. I can develop web
wonder if anyone can help; I have an a two dimensional array which I
i wonder why is this happening all the time...!! I wrote two programs one
I wonder what's the algorithm of make_heap in in C++ such that the complexity
I wonder if this would be a bad idea to makes such defines in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.