長さ、もう一方はそれを最初に見つけなければならない。その2番目はOPのルーチンよりもまだ高速です。
#include <stdio.h>
#include <string.h>
#include <time.h>
#define LEN 15001767
#define REPS 100
char array[LEN];
char *asmstrchrA(const char *s, int c)
// knows the string length
{
__asm {
push es
mov ax,ds
mov es,ax
mov edi,s
mov ecx,LEN
mov eax,c
cld
repne scasb
jz foundA
xor edi,edi ; not found
inc edi
foundA:
dec edi
mov s,edi
pop es
}
return (char *)s;
}
char *asmstrchrB(const char *s, int c)
// finds the string length first
{
__asm {
push es
mov ax,ds
mov es,ax
mov edi,s ; find string length
xor eax,eax
mov ecx,-1
cld
repne scasb
mov ecx,edi
sub ecx,s
mov edi,s ; find char
mov eax,c
cld
repne scasb
jz foundB
xor edi,edi ; not found
inc edi
foundB:
dec edi
mov s,edi
pop es
}
return (char *)s;
}
char *OPstrchr(const char *s, int c)
// from OP's link
{
while (*s != (char)c)
if (!*s++)
return 0;
return (char *)s;
}
int main (void) {
clock_t start;
int i;
char * cptr;
memset(array, '1', LEN-1);
array[LEN-5] = '2';
start = clock();
for(i=0; i<REPS; i++)
cptr = OPstrchr(array, '2');
printf("OPstrchr %p, time = %f seconds\n", (void*)cptr, (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(i=0; i<REPS; i++)
cptr = asmstrchrA(array, '2');
printf("asmstrchrA %p, time = %f seconds\n", (void*)cptr, (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(i=0; i<REPS; i++)
cptr = asmstrchrB(array, '2');
printf("asmstrchrB %p, time = %f seconds\n", (void*)cptr, (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(i=0; i<REPS; i++)
cptr = strchr(array, '2');
printf("strchr %p, time = %f seconds\n", (void*)cptr, (double)(clock() - start)/CLOCKS_PER_SEC);
return 0;
}
プログラムの出力:
OPstrchr 0125F5A2, time = 7.488000 seconds
asmstrchrA 0125F5A2, time = 1.248000 seconds
asmstrchrB 0125F5A2, time = 2.512000 seconds
strchr 0125F5A2, time = 1.045000 seconds
はおそらく、彼らは 'REPNE scasb'命令を使用して、アセンブラで書かれています。 –
標準ライブラリの名前は実装のための予約です。あなたのコードでそれらを使用しないでください。また**あなたはそれにリンクするだけでなく、**のコード**を提供する必要があります。 – Olaf
私は明らかに私の機能の名前を変更しました。私が作ったテストでは、新しい機能がなくても、私は同じ持続時間が約17msです。 – Kdmeizk