8

I have more or less the same question as

linux time command resulting real is less than user

and

user time larger than real time

but can't post a comment on those questions.

When I run the non-multi-threaded program given below, I occasionally get user time greater than real time with both /usr/bin/time and bash's builtin time. I don't see anything that might use a different core. Is rand() somehow the culprit? How? Thanks!

#include <stdio.h>
#include <stdlib.h>

#define N 100
#define MM_MAX 50000

int
main(int ac, char **av)
{
    unsigned int i, j, k, n;
    int A[N][N], B[N][N], C[N][N];

    if (ac != 2) {
        fprintf(stderr, "Usage: matmul <seed>");
        exit(1);
    }
    srand((unsigned int) atoi(av[1]));

    for (n = 0; n < atoi(av[1]); n++) {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                A[i][j] = rand() % MM_MAX;
                B[i][j] = rand() % MM_MAX;
            }
        }
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                C[i][j] = 0;
                for (k = 0; k < N; k++) {
                    C[i][j] += A[i][k] * B[k][j];
                }
                printf("%7d ", C[i][j]);
            }
            putchar('\n');
        }
    }

    return 0;
}
Community
  • 1
  • 1
  • 1
    How do you compile this code, on which machine and how do you run it? – kvantour Apr 05 '19 at 08:47
  • Can not reproduce that. I tried it on macOS with clang, on Ubuntu with gcc. Could you show the output from the command line? – Jerry Yang Apr 12 '19 at 00:58
  • are you running it on linux? check if your system has hyperthreading enabled: http://fibrevillage.com/sysadmin/155-how-to-tell-if-cpu-hyperthreading-enabled-on-linux here's some details about hyper-threading with single threaded code: https://unix.stackexchange.com/questions/18637/should-i-disable-hyperthreading-when-concerned-about-performance-of-single-threa/18645#18645 – runwuf Apr 12 '19 at 04:20

0 Answers0