Browse thread
[Caml-list] Null pointer
- Prakash Countcham
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Prakash Countcham <countcham_p.nospam@c...> |
| Subject: | [Caml-list] Null pointer |
Hi,
I wonder if it is possible to implement some optional types (and maybe other
data structures) with a null pointer when compiling in native code.
It is not possible in general, because we have to distinguish None and Some(None).
But in some cases, we know that the argument of Some can't be None.
Typically, that is the case of record types that are boxed thus can't be null.
So why not adding a keyword that make record types optional for the shake of
efficiency.
e.g.
type record1 = {...}
type optional record2 = {...}
record2 would be the same as (record1 option), but would be more efficient.
The UNIX time command on a PC linux shows a big improvement (more than 40%) in
the following example :
type recor = {nb:int; ch:string};;
let null = {nb=0; ch=""};;
let a = Some {nb=1; ch="a"};;
let b = {nb=1; ch="a"};;
for i = 1 to 100000 do
for j = 1 to 10000 do
match a with
| None -> ()
| Some c -> ignore(1+c.nb)
done
done;;
(* => 6.970u 0.010s 0:06.98 100.0% 0+0k 0+0io 97pf+0w *)
for i = 1 to 100000 do
for j = 1 to 10000 do
if not (b == null) then
ignore(1+ b.nb)
done
done;;
(* => 3.980u 0.000s 0:03.99 99.7% 0+0k 0+0io 97pf+0w *)
Thank you for your answer,
Prakash Countcham
___________________
-------------------
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