MUCH FASTER int_to_bytes, bytes_to_int, and allocate_string
- Posted by aku at inbox.as Sep 11, 2001
- 541 views
These functions is Copyright (c) 2001 Ting (alias Aku). You may use this functions freely (especially for win32 API structures). except for RDS: if RDS wants to use this replacement functions for the next version releases of Euphoria, please contact me before releasing. ------------ BENCHMARK RESULT on my computer using EXW --------- int_to_bytes: 7.634, int_to_bytes2: 4.244, 79.87747408% faster bytes_to_int: 5.744, bytes_to_int2: 3.512, 63.55353075% faster allocate_string: 6.408, allocate_string2: 5.11, 25.40117417% faster ------------ FUNCTIONS, file: machine2.e ----------------- include machine.e object ppi, pps ppi = allocate(4) global function int_to_bytes2(object x) poke4(ppi, x) return peek({ppi,4}) end function global function bytes_to_int2(sequence x) poke(ppi, x) return peek4u(ppi) end function global function allocate_string2(sequence s) atom mem mem = allocate(length(s) + 1) if mem then poke(mem, s) poke(mem+length(s), 0) end if return mem end function ------------ BENCHMARK PROGRAM ------------ include machine2.e include misc.e include dll.e global function isst(object ob) if sequence(ob) then for i=1 to length(ob) do if sequence(ob[i]) then return 0 end if end for return 1 else return 0 end if end function global procedure pc(object apapun) -- func2proc if object(apapun) then end if end procedure global procedure tln(object ob) if atom(ob) then printf(1, "%.10g\n", ob) elsif isst(ob) then puts(1, ob & "\n") else print(1, ob) puts(1, "\n") end if end procedure global function tlb(object ob) if atom(ob) then return sprintf("%.10g", ob) elsif isst(ob) then return ob else return sprint(ob) end if end function global function timer() if platform() = 2 then return (c_func(define_c_func(open_dll("kernel32.dll"), "GetTickCount", {}, C_LONG), {})/1000) else return time() end if end function atom w, z sequence y, k k = {0,0,0,0,0,0,0,0,0,0,0,0} tln("press any key") pc(getc(0)) -------- int_to_bytes w = timer() for i=1 to 500000 do y = int_to_bytes(i) end for for i=4000000000 to 4000500000 do y = int_to_bytes(i) end for k[1] = timer()-w ?y w = timer() for i=1 to 500000 do y = int_to_bytes2(i) end for for i=4000000000 to 4000500000 do y = int_to_bytes2(i) end for k[2] = timer()-w ?y tln("int_to_bytes: " & tlb(k[1]) & ", int_to_bytes2: " & tlb(k[2]) & ", " & tlb(k[1]/k[2]*100-100) & "% faster") ---------- bytes_to_int w = timer() for i=1 to 500000 do z = bytes_to_int({11,33,55,88}) end for for i=4000000000 to 4000500000 do z = bytes_to_int({250,251,252,253}) end for k[3] = timer()-w ?z w = timer() for i=1 to 500000 do z = bytes_to_int2({11,33,55,88}) end for for i=4000000000 to 4000500000 do z = bytes_to_int2({250,251,252,253}) end for k[4] = timer()-w ?z tln("bytes_to_int: " & tlb(k[3]) & ", bytes_to_int2: " & tlb(k[4]) & ", " & tlb(k[3]/k[4]*100-100) & "% faster") --------- allocate_string w = timer() for i=1 to 500000 do z = allocate_string("asjcoeuiqwjx,qjkx83uiqojsdqicuqe9qciqsxkiopqks") free(z) end for k[5] = timer()-w ?z w = timer() for i=1 to 500000 do z = allocate_string2("asjcoeuiqwjx,qjkx83uiqojsdqicuqe9qciqsxkiopqks") free(z) end for k[6] = timer()-w ?z tln("allocate_string: " & tlb(k[5]) & ", allocate_string2: " & tlb(k[6]) & ", " & tlb(k[5]/k[6]*100-100) & "% faster") tln("press any key") pc(getc(0))