Version française
Home     About     Download     Resources     Contact us    
Browse thread
Correct way of programming a CGI script
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Gerd Stolpmann <info@g...>
Subject: Re: [Caml-list] Correct way of programming a CGI script
Am Dienstag, den 09.10.2007, 07:37 +1000 schrieb skaller:
> On Mon, 2007-10-08 at 18:04 +0200, Gerd Stolpmann wrote:
> 
> > > I heard that OCaml is particularly slow (and probably
> > > memory-inefficient) when it comes to string manipulation.
> > 
> > No, this is nonsense. Of course, you can slow everything down by using
> > strings in an inappropriate way, like
> > 
> > let rec concat_list l =
> >   match l with
> >     [] -> ""
> >   | s :: l' -> s ^ concat_list l'
> 
> Now Gerd, I would not call the claim nonsense. If you can't
> use a data structure in a natural way, I'd say the claim indeed
> has some weight.

I don't know what the "nature" of strings is. I'm rather to believe they
are artifacts, and that there are several ways of defining strings
mostly resulting in different runtime behaviour.

The point is here that ^ always copies strings, and this is generally
expensive, especially in this example, because the same bytes are copied
every time the result string is extended.

I'm fully aware that you can get rid of this copying in the definition
of strings, but this has a price for some other operations. As said, you
can implement strings in various ways.

> If you consider an imperative loop to concat the strings
> in an array
> 
> 	let s = ref "" in
> 	for i = 0 to Array.length a do
> 		s := !s ^ a.[i]
> 	done;

I would call this version even uglier... But taste differs.

The point is that neither the O'Caml runtime representation of strings
nor the compiler (it could recognize the specific use of ^ and
implicitly convert the code so it uses a buffer) do anything for
avoiding this trap.

But we have to be fair. It is simply nonsense to call the whole O'Caml
string manipulation slow. You have access to all operations you need to
do it fast. You just have to know how to code it.

> then Ocaml is likely to do this slowly. C++ on the other
> hand will probably do this faster, especially if you reserve
> enough storage first.

> Now, back to the issue: in the Felix compiler itself, the
> code generator is printing out C++ code. This is primarily
> done with Ocaml string concatenation of exactly the kind which
> one might call 'inappropriate'. Buffer is used too, but only
> for aggregating large strings.
> 
> The reason, trivially, is that it is easier and clearer to write
> 
> 	"class " ^ name ^ " {\n" ^ 
> 	catmap "\n" string_of_member members ^
> 	"\n};\n"
> 
> 
> than to use the imperative Buffer everywhere. The above gives more
> clue to what the output looks like.

Well, if you only concatenate a few strings isn't going to be a problem,
and is probably as fast as using a buffer (which has also some cost).

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------