Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

using struct! to convert values to binary! datatype

 [1/6] from: rebol-list2::seznam::cz at: 16-Oct-2002 13:18


Hello rebol-list, thanks to discussion about the system-port I just found that the struct! is free in Rebol, so I started to play with it and found, that it's realy good for converting values to binary! format. For example the first thing in my %make-swf.r file - converting integer! to Unsigned 32-bit integer value: I was using this ugly function: int-to-ui32: func[i [number!]][head reverse load rejoin ["#{" to-hex to-integer i "}"]] t: now/time/precise loop 100000 [int-to-ui32 100] now/time/precise - t ;== 0:00:02.143 ;With the struct! I can do: int-to-ui32-st: func[n][third make struct! [n [integer!]] to block! n] t: now/time/precise loop 100000 [int-to-ui32-st 100] now/time/precise - t ;== 0:00:00.6 As you can see, it's much more faster, but I'm still creating new struct! - why not to use just one: ui32-struct: make struct! [value [integer!]] none int-to-ui32-st: func[i][ui32-struct/value: i copy third ui32-struct] int-to-ui32-st 100 ;== #{64000000} t: now/time/precise loop 100000 [int-to-ui32-st 100] now/time/precise - t ;== 0:00:00.19 ;Isn't it nice? It's faster! ;In the same way I can replace my old slow functions as well: int-to-ui16: func[i [number!]][head reverse load rejoin ["#{" skip mold to-hex to integer! i 5 "}"]] int-to-ui8: func[i [number!]][load rejoin ["#{" skip mold to-hex to integer! i 7 "}"]] int-to-bits: func[i [number!] bits][skip enbase/base load rejoin ["#{" to-hex to integer! i "}"] 2 32 - bits] ;to these new one: ui16-struct: make struct! [value [short]] none int-to-ui16-st: func[i][ui16-struct/value: i copy third ui16-struct] int-to-ui8-st: func[i][ui16-struct/value: i copy/part third ui16-struct 1] int-to-bits-st: func[i [number!] bits][skip enbase/base head reverse int-to-ui32-st i 2 32 - bits] t: now/time/precise loop 100000 [int-to-ui16 100] now/time/precise - t ;== 0:00:02.344 int-to-ui16-st 100 ;== #{6400} t: now/time/precise loop 100000 [int-to-ui16-st 100] now/time/precise - t ;== 0:00:00.201 t: now/time/precise loop 100000 [int-to-ui8 100] now/time/precise - t ;== 0:00:02.173 int-to-ui8-st 100 ;== #{64} t: now/time/precise loop 100000 [int-to-ui8-st 100] now/time/precise - t ;== 0:00:00.351 int-to-bits 100 16 ;== "0000000001100100" t: now/time/precise loop 100000 [int-to-bits 100 16] now/time/precise - t ;== 0:00:02.404 int-to-bits-st 100 16 ;== "0000000001100100" t: now/time/precise loop 100000 [int-to-bits-st 100 16] now/time/precise - t ;== 0:00:00.801 I'm really happy to see, that we are going faster and faster:-)) The %make-swf.r file should be uploaded with this improvement and you can just download it from here: http://oldes.multimedia.cz/swf/make-swf.r cheers Oldes

 [2/6] from: greggirwin:mindspring at: 16-Oct-2002 15:20


Hi Oldes, Excellent! Do you need to use COPY in cases where you're returning the entire element, as in: int-to-ui32-st: func[i][ui32-struct/value: i copy third ui32-struct] Eliminating COPY, if you can, should add even a little more speed. --Gregg

 [3/6] from: brett:codeconscious at: 17-Oct-2002 8:03


> thanks to discussion about the system-port I just found that the struct!
is free in Rebol, It does not work Core or in Base - so don't throw away your functions just yet. Still your findings are very handy to know. Regards, Brett.

 [4/6] from: rotenca:telvia:it at: 17-Oct-2002 1:45


Hi,
> ;With the struct! I can do: > > int-to-ui32-st: func[n][third make struct! [n [integer!]] to block! n] > t: now/time/precise loop 100000 [int-to-ui32-st 100] now/time/precise - t > ;== 0:00:00.6
You must pay attention to the little-endian big-endian:
> int-to-ui32-st 100
== #{64000000}
>> to-hex 100
== #00000064 To-hex is more fast if you need a issue/string not a binary. I find struct! very useful to convert external data from standard C integer/float/double format to Rebol value. --- Ciao Romano

 [5/6] from: rebol-list2:seznam:cz at: 19-Oct-2002 10:19


Hello Brett, Thursday, October 17, 2002, 12:03:59 AM, you wrote:
>> thanks to discussion about the system-port I just found that the struct!
BH> is free in Rebol, BH> It does not work Core or in Base - so don't throw away your functions just BH> yet. BH> Still your findings are very handy to know. BH> Regards, BH> Brett. Hmm... that's right, I started to make the Rebol/Flash dialect for Core but now I use it only in Rebol/View 1.2.1 (because I didn't fixed the %img-to-bll.r script to be working in the new Rebol/View yet and I most cases I need to work with images:( I will add some version testing.

 [6/6] from: rebol-list2:seznam:cz at: 19-Oct-2002 10:26


Hello Gregg, Wednesday, October 16, 2002, 11:20:49 PM, you wrote: GI> Hi Oldes, GI> Excellent! Do you need to use COPY in cases where you're returning the GI> entire element, as in: GI> int-to-ui32-st: func[i][ui32-struct/value: i copy third ui32-struct] GI> Eliminating COPY, if you can, should add even a little more speed. GI> --Gregg In most cases it is needed. I was using it without copy, but I had some related bugs. Look at this example:
>> ui32-struct: make struct! [value [integer!]] none >> int-to-ui32-st: func[i][ui32-struct/value: i copy third ui32-struct] >> int-to-ui32-st 100
== #{64000000}
>> rejoin [int-to-ui32-st 1 int-to-ui32-st 2]
== #{0100000002000000}
>> int-to-ui32-st: func[i][ui32-struct/value: i third ui32-struct] >> rejoin [int-to-ui32-st 3 int-to-ui32-st 4]
== #{0400000004000000} I'm not sure, if it's not a Rebol bug, maybe it's just by design.