Ich habe ein Problem mit memcpy, das ich nicht zu verstehen scheinen. Ich benutze memcpy auf einem Thread und es ist 3-4 mal langsamer im Vergleich zu der Zeit, die ich bekomme, wenn ich es von der Hauptleitung aus führe. In beiden Fällen habe ich 2 Threads, von denen einer nur wartet und einer memcpy aufruft. Können Sie mir eine mögliche Erklärung dafür geben? Ich benutze eine 4-Core-Intel-Maschine mit Hyperthreading.memcpy langsam beim Aufruf in Thread
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <pthread.h>
#include <algorithm>
#define MILLION 1000000
#define START_TIMER(timer) { \
gettimeofday(&(timer), NULL); \
}
#define STOP_TIMER(timer) { \
gettimeofday(&(timer), NULL); \
}
#define TIME_DIFF(timer1, timer2, total) { \
long long sec_diff = 0; \
long long usec_diff = 0; \
sec_diff = (timer2).tv_sec - (timer1).tv_sec; \
usec_diff = (timer2).tv_usec - (timer1).tv_usec; \
(total)+= (sec_diff * MILLION) + usec_diff; \
}
void copy(){
struct timeval start, stop;
long long total=0;
char buff[1024*1024];
for(int i =0;i<100;i++){
char* temp = new char[1024*1024];
START_TIMER(start);
std::copy(buff,buff+1024*1024,temp);
STOP_TIMER(stop);
TIME_DIFF(start,stop,total);
delete temp;
}
printf("%lld\n",total/100);
}
void* mem(void* args){
copy();
pthread_exit(NULL);
}
void * nothing(void *args){
pthread_exit(NULL);
}
pthread_t thread;
int main(int argc,char* argv[]){
if(atoi(argv[1])==0){
pthread_create(&thread,NULL,nothing,NULL);
pthread_join(thread,NULL);
copy();
}
else{
pthread_create(&thread,NULL,mem,NULL);
pthread_join(thread,NULL);
}
}
Vielen Dank für Ihre Zeit. Ich hoffe es ist nicht zu dumm.
Was passiert, wenn Sie 'buff' vor Ihrer' i' Schleife initialisieren? – 1201ProgramAlarm
Bitte teilen Sie den Code, wo Sie die Kopierfunktion von Haupt und von den Threads aufrufen. Damit wir versuchen können, das Problem zu reproduzieren. – Sush
Was passiert, wenn Sie die copy() in main() über dem Thread create verschieben, dh zuerst die Hauptthread-Kopie ausführen? – ThingyWotsit