Why is memset slow?

This is the place to discuss the latest computer hardware issues and technology. Please keep the discussion ON TOPIC!
Post Reply
Jordan350
Legit Little One
Legit Little One
Posts: 1
Joined: Thu Jan 10, 2019 12:39 am

Why is memset slow?

Post by Jordan350 » Sat Mar 09, 2019 2:55 am

The spec for my CPU says it should get 5.336GB/s bandwidth to memory. To test this, I wrote a simple program that runs memset (or memcpy) on a big array and reports the timing. I'm showing 3.8GB/s on memset and 1.9GB/s on memcpy tutuapp.software. http://en.wikipedia.org/wiki/Intel_Core ... hitecture) says my Q9400 should be getting 5.336MB/s. What's wrong?

I've tried replacing memset or memcpy with assignment loops. I've googled around to try to learn about memory alignment. I've tried different compiler flags. I've spent an embarrassing number of hours on this. Thanks for any help you can provide!

I'm using Ubuntu 12.04 with libc-dev version 2.15-0ubuntu10.5 and kernel 3.8.0-37-generic

The code:

Code: Select all

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

#define numBytes ((long)(1024*1024*1024))
#define numTransfers ((long)(8))

int main(int argc,char**argv){
    if(argc!=3){
        printf("Usage: %s BLOCK_SIZE_IN_BYTES NUMBER_OF_BLOCKS_TO_TRANSFER\n",argv[0]);
        return -1;
    }
    char*__restrict__ source=(char*)malloc(numBytes);
    char*__restrict__ dest=(char*)malloc(numBytes);
    struct timespec start,end;
    long totalTimeMs;
    int i;

    clock_gettime(CLOCK_MONOTONIC_RAW,&start);
    for(i=0;i<numTransfers;++i)
        memset(source,0,numBytes);
    clock_gettime(CLOCK_MONOTONIC_RAW,&end);
    totalTimeMs=(end.tv_nsec-start.tv_nsec)*.000001+1000*(end.tv_sec-start.tv_sec);
    printf("memset %ld bytes %ld times (%.2fGB total) in %ldms (%.3fGB/s). ",numBytes,numTransfers,numBytes/1024.0/1024/1024*numTransfers,totalTimeMs,numBytes/1024.0/1024/1024*1000*numTransfers/totalTimeMs);

    clock_gettime(CLOCK_MONOTONIC_RAW,&start);
    for(i=0;i<numTransfers;++i)
        memcpy( dest, source, numBytes);
    clock_gettime(CLOCK_MONOTONIC_RAW,&end);
    totalTimeMs=(end.tv_nsec-start.tv_nsec)*.000001+1000*(end.tv_sec-start.tv_sec);
    printf("memcpy %ld bytes %ld times (%.2fGB total) in %ldms (%.3fGB/s).\n",numBytes,numTransfers,numBytes/1024.0/1024/1024*numTransfers,totalTimeMs,numBytes/1024.0/1024/1024*1000*numTransfers/totalTimeMs);

    free(source);
    free(dest);

    return EXIT_SUCCESS;
}
Last edited by Jordan350 on Tue Mar 12, 2019 1:38 am, edited 1 time in total.

User avatar
KnightRid
Moderator
Moderator
Posts: 3842
Joined: Wed Dec 14, 2005 5:21 am
Location: Dallastown, PA

Re: Why is memset slow?

Post by KnightRid » Sat Mar 09, 2019 5:55 am

Sorry I can not be of great help. You are leagues above me!

Maybe look at your motherboard and any limitations it has in regards to slowing down the memory process going to and from the cpu...? It should not be slowing it down that much I wouldn't think....

DAMMIT - https://stackoverflow.com/questions/233 ... emset-slow

4 years and 10 months ago it was asked....so why would a bot re-post this? Weird.
Cigar Blog I review for - http://www.stogiereview.com

Remember, I am opinionated and nothing I say or do reflects on anyone or anything else but me :finga:

User avatar
Kaos Kid
Legit Extremist
Legit Extremist
Posts: 932
Joined: Sat Mar 27, 2010 11:31 am
Location: 40 clicks West of the Gateway

Re: Why is memset slow?

Post by Kaos Kid » Sun Mar 10, 2019 11:31 pm

Also weird that it took almost two months after it joined to make this post. The booger bots are working on the long game now.
I have come to the conclusion that "FaceBook" should be renamed "FacePalm" :roll:

Post Reply