From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA10982; Tue, 4 Mar 2003 19:39:33 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA11465 for ; Tue, 4 Mar 2003 19:39:32 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h24Id0T04244; Tue, 4 Mar 2003 19:39:00 +0100 (MET) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA10964; Tue, 4 Mar 2003 19:39:00 +0100 (MET) Date: Tue, 4 Mar 2003 19:38:59 +0100 From: Xavier Leroy To: "William D. Neumann" Cc: caml-list@inria.fr Subject: Re: [Caml-list] [RANT] String representation (was: Strings as arrays or lists...) Message-ID: <20030304193859.A10907@pauillac.inria.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: ; from wneumann@cs.unm.edu on Tue, Mar 04, 2003 at 09:14:28AM -0700 X-Spam: no; 0.00; caml-list:01 rant:01 shifts:01 ors:99 char:01 slower:01 arrays:01 unsafe:01 ints:01 byte:01 int:01 ops:01 bytes:02 external:03 unit:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Unfortunately strings don't act like byte arrays (to the best of my > knowledge anyway -- I'd love to find out I'm wrong). If you want to do > any low level operations on the bytes, you first need to convert the chars > to ints (and by low level, I'm talking about shifts, rotates, ands, ors, > xors, etc), perform your ops, then convert back to chars before > re-inserting them into the string, and this is slooooow. Actually, Char.code (the conversion from char to int) is a no-op, but it's true that Char.chr (the conversion from int to char) is slower because it checks that its argument lies within [0..255]. An alternative is Char.unsafe_chr, which is a no-op (but doesn't perform the range check). A better alternative is to declare external get_byte: string -> int -> int = "%string_safe_get" external set_byte: string -> int -> int -> unit = "%string_safe_set" and use these two functions to access strings as if they were byte arrays. set_byte will store the low 8 bits of its third argument, so you'd save on "land 0xFF" operations too. > Since I do crypto for a living, I run into these problems all the time. Sure. The two declarations above should help both with performance and legibility of your code. - Xavier Leroy ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners