Hirdetés

Új hozzászólás Aktív témák

  • pmonitor

    aktív tag

    válasz Fire/SOUL/CD #16427 üzenetére

    Még azt mondják, hogy nem lehet assemblyvel gyorsabb kódot írni! Az olyanoknak ajánlom ezt a példakódot a figyelmébe:
    #pragma once
    #define _CRT_SECURE_NO_WARNINGS

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <sys/timeb.h>

    int int_ToString(int num, char* dest);
    char str[12];

    long timediff(struct timeb* start, struct timeb* end)
    {
    long seconds;
    seconds = (long)(end->time - start->time);
    start->millitm = end->millitm - start->millitm;
    if (0 > start->millitm) {
    start->millitm += 1000;
    seconds--;
    }
    return seconds;
    }

    int main(void)
    {
    struct timeb start, end;
    long seconds, seconds_1;
    int militm, militm_1;
    int i;
    ftime(&start);
    for (i = 0; i < 100000000; i++)
    {
    sprintf(str, "%d", -2138);
    //printf(str);
    }
    ftime(&end);
    seconds = timediff(&start, &end);
    militm = start.millitm;

    ftime(&start);
    for (i = 0; i < 100000000; i++)
    {
    int_ToString(-2138, str);
    //printf(str);
    }
    ftime(&end);
    seconds_1 = timediff(&start, &end);
    militm_1 = start.millitm;
    printf("Eltelt ido(sprintf()): %ld.%03d masodperc\n", seconds, militm);
    printf("Eltelt ido(assembly): %ld.%03d masodperc\n", seconds_1, militm_1);
    }


    int int_ToString(int num, char* dest)
    {
    __asm
    {
    mov edi, dest
    mov eax, num
    xor edx, edx
    cmp eax, edx
    jge nemnegativ
    mov ecx, -1
    imul ecx
    push eax
    mov eax, '-'
    stosb
    pop eax

    nemnegativ :
    xor ebx, ebx
    push_chars :
    xor edx, edx
    mov ecx, 10
    div ecx
    add edx, 0x30
    push edx
    inc ebx
    test eax, eax
    jnz push_chars
    pop_chars :
    pop eax
    stosb
    dec ebx
    cmp ebx, 0
    jg pop_chars
    mov eax, 0x0a
    stosb
    }
    }

    Nálam itt az sprintf(...) stabilan 6 sec felett, az assembly kód stabilan 2 sec. alatt teljesített. Csupán kevesebb, mint az egyharmada alatt fut le az ASM kód. :DD

Új hozzászólás Aktív témák