0

I am trying to find the time taken by memmove function in c using time.h library. However, When i execute the code, I get the value as zero. Any possible solution to find the time taken by the memmove function?

void main(){
uint64_t start,end;
uint8_t a,b;
char source[5000];
char dest[5000];
uint64_t j=0;

for(j=0;j<5000;j++){
source[j]=j;
}

start=clock();
memmove(dest,source,5000);
end=clock();
printf("%f",((double)end-start));
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Please indent your code (like the samples in your C text book). – Jabberwocky Dec 08 '17 at 15:54
  • You get the value zero because memmoving 5000 bytes is probably faster then the resulution of your clock. Tell us more about your platform. – Jabberwocky Dec 08 '17 at 15:55
  • Do more work. It doesn’t take long enough to copy 5,000 characters for it to be measurable with `clock()`. Or consider using `clock_gettime()`. – Jonathan Leffler Dec 08 '17 at 15:55
  • The code is still not indented. – Jabberwocky Dec 08 '17 at 15:55
  • I have tried the above code on my host pc which is 64 bit windows operating system with i7 processor having 2.7 GHz clock. Also, I have tried the code on my beaglebone board which has ubuntu operating system and has clock of 1Ghz – vikrant waje Dec 08 '17 at 15:57
  • In case, I do more work, I might get the time in finite seconds, but that time wont be solely for time required for memmove ,right? @Jonathan Leffler – vikrant waje Dec 08 '17 at 15:58

2 Answers2

1

As I write in my comment, memmoving 5000 bytes is far too fast to be mesurable with clock. If you do your memmove 100000 times, then it will get mesurable.

This code below gives an output of 12 on my computer. But this is platform dependent, the number you get on your computer might be quite different.

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

int main(void) {
  uint64_t start, end;
  char source[5000];
  char dest[5000];
  uint64_t j = 0;

  for (j = 0; j < 5000; j++) {
    source[j] = j;
  }

  start = clock();

  for (int i = 0; i < 100000; i++)
  {
    memmove(dest, source, 5000);
  }

  end = clock();
  printf("%lld", (end - start));  // no need to convert to double, (end - start)
                                  // is an uint64_t.
 }
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • This works with Visual Studio, but doesn't appear to work with gcc (release mode). Probably gcc optimization ignores the loop, because the loop does not affect the program result. Also `clock()` depends on `CLOCKS_PER_SEC` which is `1,000` in windows, in *nix I think is usually `1,000,000` – Barmak Shemirani Dec 09 '17 at 02:34
  • you can turn off optimization when compiling – Flying Swissman Dec 09 '17 at 09:02
0

If you want to know the time it takes on a beagle bone or another device with a GIPO you can toggle a GPIO before and after your routine. You will have to hook up an oscilloscope or something similar that can sample voltage quickly.

I don't know much about beagle bones but it seems the library libpruio allows fast gpio toggling.

Also, what is your exact goal here? Compare speed on different hardware? As someone suggests, you could increase the number of loops so it becomes more easily measurable with time.h.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Flying Swissman
  • 818
  • 2
  • 14
  • 39