Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] printable digest strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: Re: [Caml-list] printable digest strings

>Or in a more imperative style (real programmers don't build
>intermediate lists :-)

Real programmers prematurely optimize silly little functions (code below) and don't call sprintf per character:

:)

native code

test    secs
----    ----
hex     0.38
hexu    0.16
hexx    7.30

bytecode

test    secs
----    ----
hex      2.75
hexu     2.31
hexx    27.24


> string_map

On a more serious note/question, you define string_map as:

string_map : (char -> 'a) -> string -> 'a list

Since I'm still trying to grok this functional thing, I guess there's really no other way to do it (except maybe 'a array) because the function could expand its char into another type, right?  In other words, other maps are 

map: ('a -> 'b) -> 'a type -> 'b type

so there's a certain amount of symmetry.  Since string is a builtin, there's no way to have a "float string" or whatever if your function is char -> float.  Is 'a array the right thing to return here, or 'a list?  Or is this a stupid irrelevant question?

Chris

PS.  It's pretty clear from looking at the asm output of hexu that languages with shifted-up ints sure could benefit from crazy shift-mask instructions like the PPC has.  The x86 just dies on this sort of thing with no ternary instructions and no good shift-mask ops.


--- test harness ---
let _ =
  let s = String.make 10000 'g' in
  let r = ref "" in
  for i = 0 to 100 do
    r := hexstring s;
  done


---- hex ----
let hexstring s =
  let res = String.create (String.length s * 2) in
  let hex = "0123456789abcdef" in
  for i = 0 to String.length s - 1 do
    let c = Char.code s.[i] in
    res.[2*i] <- hex.[c lsr 4]; res.[2*i+1] <- hex.[c land 15]
  done;
  res

---- hexu ----
let hexstring s =
  let res = String.create (String.length s * 2) in
  let hex = "0123456789abcdef" in
  for i = 0 to String.length s - 1 do
    let c = Char.code (String.unsafe_get s i) in
    String.unsafe_set res (2*i)   (String.unsafe_get hex (c lsr 4));
    String.unsafe_set res (2*i+1) (String.unsafe_get hex (c land 15));
  done;
  res

---- hexx ----

let hexstring s =
  let res = String.create (String.length s * 2) in
  for i = 0 to String.length s - 1 do
    String.blit (Printf.sprintf "%02x" (Char.code s.[i])) 0
                res (2 * i) 2
  done;
  res


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr