Skip to main content

Command Palette

Search for a command to run...

Faster Variables?

Updated
2 min read
J

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.

At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.

In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.

The register keyword in C is a hint to the compiler that a particular variable should be stored in a CPU register instead of in memory. This is used to optimize performance, as accessing a CPU register is typically faster than accessing memory. However, it's important to note that the use of register is just a suggestion to the compiler; modern compilers may ignore this hint if they determine that it won't improve performance.

When to Use register:

  1. Frequently Accessed Variables: Use register for variables that are frequently accessed, such as loop counters.

  2. Small and Fast Operations: Ideal for variables involved in small, fast operations where the overhead of memory access can be a bottleneck.

When Not to Use register:

  1. Large Variables: Avoid using register for large data structures or arrays, as they can't be fit into a register.

  2. Pointer Variables: Generally, it's not advisable for pointers, since modern compilers are typically better at optimization.

  3. Address Required: If you need the address of a variable, don’t use register, as you can’t take the address of a register.

#include <stdio.h>

int main() {
    // Using register for a loop counter
    register int i;

    for (i = 0; i < 10; i++) {
        printf("%d\n", i);
    }

    return 0;
}

Explanation:

  • Here, the variable i is suggested to be stored in a register for faster access, which can be beneficial in the loop for incrementing and checking the loop counter.

  • This is a typical scenario where register might be useful, especially in older or less sophisticated compilers.

  • However, it's worth noting that modern compilers are quite good at optimizing such uses even without the register keyword.

More from this blog

Jyotiprakash's Blog

251 posts

I'm Jyotiprakash, a software dev and professor at KIIT, with expertise in system programming.