Register Variable Test

Register Variable Test

Hard C Variables 27 views
Explanation Complexity

Problem Statement

Use a normal variable and a register variable in a loop. Increment both by 1 million. Measure the time difference (practical experiment).

Input Format

A loop that runs 1,000,000 times.
One normal variable and one register variable are used.

Output Format

Time taken by:

• normal variable

• register variable

Example

Loop count = 1,000,000
Normal variable time  : slightly higher
Register variable time: slightly lower

Constraints

• Same loop count

• Same operations

• Same system and compiler

Concept Explanation

register variable tells the compiler to keep the variable in CPU register instead of RAM.

Step-by-Step Explanation

• Create a normal integer variable.

• Create a register integer variable.

• Run a loop from 1 to 1,000,000.

• Increment normal variable inside the loop.

• Measure time taken.

• Run the same loop again.

• Increment register variable inside the loop.

• Measure time taken again.

• Compare both times.

Concept Explanation

register variable tells the compiler to keep the variable in CPU register instead of RAM.

Step-by-Step Explanation

• Create a normal integer variable.

• Create a register integer variable.

• Run a loop from 1 to 1,000,000.

• Increment normal variable inside the loop.

• Measure time taken.

• Run the same loop again.

• Increment register variable inside the loop.

• Measure time taken again.

• Compare both times.

Input / Output Format

Input Format
A loop that runs 1,000,000 times.
One normal variable and one register variable are used.
Output Format
Time taken by:

• normal variable

• register variable
Constraints
• Same loop count

• Same operations

• Same system and compiler

Examples

Input:
Loop count = 1,000,000
Output:
Normal variable time : slightly higher Register variable time: slightly lower

Example Solution (Public)

C
#include <stdio.h>
#include <time.h>

int main() {
    int i;
    int normal = 0;
    register int reg = 0;

    clock_t start, end;
    double time_normal, time_register;

    /* ---- Normal variable loop ---- */
    start = clock();
    for (i = 0; i < 1000000; i++) {
        normal++;
    }
    end = clock();
    time_normal = (double)(end - start) / CLOCKS_PER_SEC;

    /* ---- Register variable loop ---- */
    start = clock();
    for (i = 0; i < 1000000; i++) {
        reg++;
    }
    end = clock();
    time_register = (double)(end - start) / CLOCKS_PER_SEC;

    printf("Normal variable value   = %dn", normal);
    printf("Register variable value = %dnn", reg);

    printf("Time taken by normal variable   = %f secondsn", time_normal);
    printf("Time taken by register variable = %f secondsn", time_register);

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <time.h>

int main() {
    int i;
    int normal = 0;
    register int reg = 0;

    clock_t start, end;
    double time_normal, time_register;

    /* ---- Normal variable loop ---- */
    start = clock();
    for (i = 0; i < 1000000; i++) {
        normal++;
    }
    end = clock();
    time_normal = (double)(end - start) / CLOCKS_PER_SEC;

    /* ---- Register variable loop ---- */
    start = clock();
    for (i = 0; i < 1000000; i++) {
        reg++;
    }
    end = clock();
    time_register = (double)(end - start) / CLOCKS_PER_SEC;

    printf("Normal variable value   = %dn", normal);
    printf("Register variable value = %dnn", reg);

    printf("Time taken by normal variable   = %f secondsn", time_normal);
    printf("Time taken by register variable = %f secondsn", time_register);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.