Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Chris Hecker <checker@d6.com>
To: Xavier Leroy <Xavier.Leroy@inria.fr>, Miles Egan <miles@caddr.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] printable digest strings
Date: Fri, 04 May 2001 13:27:36 -0700	[thread overview]
Message-ID: <4.3.2.7.2.20010504125129.00e25990@shell16.ba.best.com> (raw)
In-Reply-To: <20010504113727.A9728@pauillac.inria.fr>


>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


  parent reply	other threads:[~2001-05-04 20:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-03 22:32 Miles Egan
2001-05-04  9:15 ` Hendrik Tews
2001-05-04  9:37 ` Xavier Leroy
2001-05-04 14:00   ` Miles Egan
2001-05-04 14:50     ` Brian Rogoff
2001-05-04 20:27   ` Chris Hecker [this message]
2001-05-04 22:54     ` Miles Egan
2001-05-06 18:56     ` [Caml-list] Wserver: Values of global variables lost Mattias Waldau
2001-05-07  8:58       ` Daniel de Rauglaudre
2001-05-07 17:00       ` Mattias Waldau
2001-05-07 18:16         ` Daniel de Rauglaudre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4.3.2.7.2.20010504125129.00e25990@shell16.ba.best.com \
    --to=checker@d6.com \
    --cc=Xavier.Leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=miles@caddr.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox