Here it is can you help make the asm part faster?
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>


int strsrch(char* srchin,char* srch,int start=0)
{
int alen=strlen(srchin),blen=strlen(srch),numr=0;

if (blen>alen)
return -2;

for(int a=start; a<alen; a++)
{
if (srchin==srch)
{
numr++;

if (numr==blen)
return a-blen+1;
}
else
{
if (srch[0]==srchin)
a--;

numr=0;
}
}

return -1;
}

int strsrcha(char* srchin,char* srch,int start=0) // this suff here needs to run faster
{
int alen=strlen(srchin),blen=strlen(srch),numr=0,a=0;

_asm
{
mov eax,blen
cmp eax,alen
jbe no2
}
return -2;
no2:

_asm
{
mov edi,srch
mov ah,
mov esi,srchin
add esi,start

mov ecx,blen
add ecx,srch
mov edx,alen
add edx,srchin
strscrchloop:

mov al,

cmp al,
jne else1

inc edi

cmp edi,ecx
jne endofloop
sub esi,srchin
mov a,esi
jmp backr
else1:
mov edi,srch

cmp al,ah
jne endofloop
inc edi
endofloop:
inc esi
cmp esi,edx
jne strscrchloop
}

return -1;
backr:
return a-blen+1;

}

void main()
{
char lookin[1000000];
char lookfor[]="josh";
int a,time,test1,test2;

memset(lookin,' ',1000000);

lookin[999999]=0;
lookin[999998]='h';
lookin[999997]='s';
lookin[999996]='o';
lookin[999995]='j';

Sleep(100);

time=GetTickCount();
for(a=0; a<10; a++)
test1=strsrch(lookin,lookfor);

printf("%d\n",GetTickCount()-time);
Sleep(100);
time=GetTickCount();

for(a=0; a<10; a++)
test2=strsrcha(lookin,lookfor);

printf("%d\ntest1=%d\ntest2=%d\n",GetTickCount()-time,test1,test2);
}
Got any ideas?
Posted on 2003-05-06 21:59:11 by Josh160
I can see a couple of places in your code where the compiler should fail, because you are not specifying the WORD PTR typecast. If this isn't failing, then the compiler is being far too nice to you ;)

Instead of asking for us to make that piece of (hard to read) code faster, why don't you head over to the algorithms forum, find the substring search algos there, and compare yours to those?
Posted on 2003-05-07 00:24:18 by sluggy
Posted on 2003-05-07 02:40:43 by roticv
Those post didn't really help mov al,seems to go really slow i put inc eax (witch takes the same 1cc and it like mov al, is about 8cc). Is there something faster than mov al,?
Posted on 2003-05-07 08:18:04 by Josh160
Sorry I misread your post
try this, but becareful. I was too lazy to preserve registers :rolleyes:


strsrch proc;srchin:dword, srch
mov edi, [esp+4] ; srchin
mov esi,[esp+8] ; srch
invoke strlen,edi
xchg eax,edx ; edx = alen
invoke strlen,esi ;eax = blen
mov [esp+4],eax ;blen - thrashing the para
cmp eax,edx
ja @F
mov eax,-2
ret 8
@@:
xor ecx,ecx
xor ebp,ebp ;ebp = numr
loop_:
mov al,[edi+ecx]
cmp al, byte ptr[edi+ebp]
jnz @F
inc ebp
@@:
cmp ebp, [esp+4]
jnz @F
sub ecx,[esp+4]
inc ecx
xchg ecx,eax
ret 8
@@:
mov al,[edi]
cmp al,[edi+ecx]
jnz @F
dec ecx
@@:
xor ebp,ebp
cmp ecx,edx
jnz loop_
xor eax,eax
dec eax
ret 8
strsrch endp


Sorry made a mistake, try this (still untested and did not preserve registers, coded in notepad)


strsrch proc;srchin:dword, srch
mov edi, [esp+4] ; srchin
mov esi,[esp+8] ; srch
invoke strlen,edi
xchg eax,edx ; edx = alen
invoke strlen,esi ;eax = blen
mov [esp+4],eax ;blen - thrashing the para
cmp eax,edx
ja @F
mov eax,-2
ret 8
@@:
xor ecx,ecx
xor ebp,ebp ;ebp = numr
loop_:
mov al,[edi+ecx]
cmp al, byte ptr[edi+ebp]
jnz else__
inc ebp
cmp ebp, [esp+4]
jnz endloop_
sub ecx,[esp+4]
inc ecx
xchg ecx,eax
ret 8
else___:
mov al,[edi]
cmp al,[edi+ecx]
jnz @F
dec ecx
@@:
xor ebp,ebp
loopend_:
cmp ecx,edx
jnz loop_
xor eax,eax
dec eax
ret 8
strsrch endp
Posted on 2003-05-07 11:15:59 by roticv
no expert myself but at this point u mite find agner fog's asm optimisation manual for the intel pentium processors useful, u can find it here

http://www.agner.org/assem/
Posted on 2003-05-09 00:36:40 by AnotherWay83
could you change the code so it works with a c++ compiler i can't get it to compile
Posted on 2003-05-11 00:12:40 by Josh160