Leerzeichen entfernen
-
also ich kanns ja nicht glauben
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MICRO_IN_SEC 1000000.00 double microtime(){ struct timeval tp = {0}; gettimeofday(&tp, NULL); return (double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC); } void normal(char *p,char *s){ do{ if(*s!=' ') *p++=*s; }while(*s++); } void inplace(char *s){ char *p = s; do{ if(*s!=' ') *p++=*s; }while(*s++); } int main(void) { //starttime double startTime; double sumInplace = 0; double sumNormal = 0; //startval char temp[] = "asdasdasdasdasasdasdasdasdas"; //create Array char a[100000][sizeof(temp)]; char b[100000][sizeof(temp)]; int runs = 600; int i=100000; while(runs--){ //init i=100000; while(i--){ strcpy(a[i],temp); strcpy(b[i],temp); } //inplace startTime = microtime(); i=100000; while(i--){ inplace(a[i]); } sumInplace += microtime()-startTime; //normal startTime = microtime(); i=100000; while(i--){ normal(b[i],temp); } sumNormal += microtime()-startTime; } printf("inplace:%f\n",sumInplace); printf("normal:%f\n",sumNormal); return 0; }
und das ergebnis:
-o3 schrieb:
inplace:4.795143
normal:3.412512-o2 schrieb:
inplace:4.782671
normal:4.481137evtl. hab ich auch was entscheidendes falsch gemacht, kann mir bitte jemand erklären warum die kopie variante schneller ist als inplace, muß ja nen grund haben?
lg lolo
-
noobLolo schrieb:
evtl. hab ich auch was entscheidendes falsch gemacht, kann mir bitte jemand erklären warum die kopie variante schneller ist als inplace, muß ja nen grund haben?
schau dir doch den assembler-output an, vielleicht ist da was zu erkennen. wenn dein OS während der messung task-switches macht und interrupt-routinen abarbeitet, kannste die ergebnisse sowieso knicken. ein system mit sich selbst zu messen ist immer ein bisschen schwierig.
-
also erklären kann ichs mir immer noch nicht, bin aber auch kein asm spezialist, kann es evtl. sein das die normale variante besser mit der pipeline zusammen arbeitet?
oder hängt es wie ;fricky sagte mit den task-switches und interrupt-routinen zusammen?
das schaut nach einer langen nacht ausder objdump schaut so aus
08048420 <normal>: 8048420: 55 push %ebp 8048421: 89 e5 mov %esp,%ebp 8048423: 8b 55 0c mov 0xc(%ebp),%edx 8048426: 8b 4d 08 mov 0x8(%ebp),%ecx 8048429: 83 c2 01 add $0x1,%edx 804842c: eb 08 jmp 8048436 <normal+0x16> 804842e: 66 90 xchg %ax,%ax 8048430: 83 c1 01 add $0x1,%ecx 8048433: 83 c2 01 add $0x1,%edx 8048436: 0f b6 42 ff movzbl -0x1(%edx),%eax 804843a: 3c 20 cmp $0x20,%al 804843c: 74 f5 je 8048433 <normal+0x13> 804843e: 88 01 mov %al,(%ecx) 8048440: 80 7a ff 00 cmpb $0x0,-0x1(%edx) 8048444: 75 ea jne 8048430 <normal+0x10> 8048446: 5d pop %ebp 8048447: c3 ret 08048450 <inplace>: 8048450: 55 push %ebp 8048451: 89 e5 mov %esp,%ebp 8048453: 8b 45 08 mov 0x8(%ebp),%eax 8048456: 8d 48 01 lea 0x1(%eax),%ecx 8048459: eb 0b jmp 8048466 <inplace+0x16> 804845b: 90 nop 804845c: 8d 74 26 00 lea 0x0(%esi),%esi 8048460: 83 c0 01 add $0x1,%eax 8048463: 83 c1 01 add $0x1,%ecx 8048466: 0f b6 51 ff movzbl -0x1(%ecx),%edx 804846a: 80 fa 20 cmp $0x20,%dl 804846d: 74 f4 je 8048463 <inplace+0x13> 804846f: 88 10 mov %dl,(%eax) 8048471: 80 79 ff 00 cmpb $0x0,-0x1(%ecx) 8048475: 75 e9 jne 8048460 <inplace+0x10> 8048477: 5d pop %ebp 8048478: c3 ret
evtl. mach ich mich mal dran und zähl die zyklen
lg lolo