Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
one-time initialization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Hicks <mwh@d...>
Subject: one-time initialization
I wonder if anyone knows how to optimize the following (simplified for the
sake of dicussion) situation:

let global = ref None
let init i =
  global := Some i
let f () =
  match (!global) with
    Some x -> x
  | None -> failwith "not initialized";;
let g() =
  match (!global) with

Essentially, there is some global state that is initialized once, and is
used by all functions in the module.  In a more realistic situation, this
state might be initialized by reading in a file.  Given that following
initialization the global state never changes, it should be conceivable to
eliminate the match and dereference; on my machine (pentium 166), the match
and dereference result in about a 30% slowdown.  I've fooled around with
some things, but haven't found anything that performs better than this
straightforward approach or is any more elegant.

Michael Hicks
Ph.D. Candidate, the University of Pennsylvania            mailto://
"I worked with an individual who plugged his power strip back into itself
and for the life of him could not understand why his computer would not
turn on."