Commit fe478237 authored by Pietro Abate's avatar Pietro Abate

[r2003-06-07 16:46:03 by cvscast] Benchmarks

Original author: cvscast
Date: 2003-06-07 16:46:22+00:00
parent c010efa9
split:
ocaml unix.cma bench.ml | tee split.results
open Printf
let gen = "split.ml"
let xml = "split"
let cduce = [ "split0.cd"; "split1.cd"; "split2.cd" ]
let xslt = [ "split1.xsl"; "split2.xsl" ]
let xduce = [ "split1.q"; "split2.q" ]
let sp = sprintf
let langs =
[
"CDuce", cduce,
(fun script xml ->
sp "../cduce --quiet %s --arg %s" script xml);
"XDuce", xduce,
(fun script xml ->
sp "/home/frisch/xduce-0.4.0/xduce.opt %s %s" script xml);
"XSLT", xslt,
(fun script xml ->
sp "xsltproc --noout %s %s" script xml);
]
let pr = printf
let rep = 1
let time s =
let s = "/usr/bin/time --format=\"%e,%U\\n\" " ^ s ^ " 2>&1" in
flush stdout;
for i = 1 to rep do
let ic = Unix.open_process_in s in
let t = input_line ic in
ignore (Unix.close_process_in ic);
pr "%s " t;
flush stdout;
done;
pr "\n"
let args = [ 10 ; 50; 100; (* 500; 1000; 5000 *) ]
let run s =
flush stdout;
ignore (Sys.command s)
let () =
List.iter
(fun s ->
let fn = sp "%s.%i.xml" xml s in
run (sp "ocaml %s %i > %s" gen s fn);
let ic = open_in fn in
let size = in_channel_length ic in
close_in ic;
pr "XML size = %i\n" size;
List.iter
(fun (lang, scripts, cmd) ->
List.iter
(fun script ->
pr "%6s[%20s] " lang script;
time (cmd script fn)
) scripts
)
langs;
pr "====================================\n"
)
args
let random_string n =
let s = String.create n in
for i = 0 to n - 1 do
s.[i] <- Char.chr (65 + (Random.int 26))
done;
s;;
let nb = int_of_string Sys.argv.(1);;
print_string "<?xml version=\"1.0\" encoding=\"UTF-8\"?><doc>";;
let rec person p =
Printf.printf "<person gender=\"%s\"><name>%s</name><children>\n"
(if Random.int 2 = 0 then "M" else "F")
(random_string 20);
if p < 5 then for i = 1 to Random.int 5 do person (p+1) done;
Printf.printf "</children></person>\n";;
for i = 1 to nb do
person 0;
done;;
print_string "</doc>";;
type Person = FPerson | MPerson
type FPerson = <person gender = "F" >[ Name Children ]
type MPerson = <person gender="M">[ Name Children ]
type Children = <children>[Person*]
type Name = <name>[ PCDATA ]
type Man = <man name=String>[ Sons Daughters ]
type Woman = <woman name=String>[ Sons Daughters ]
type Sons = <sons>[ Man* ]
type Daughters = <daughters>[ Woman* ]
let split (MPerson -> Man ; FPerson -> Woman)
<person gender=g>[ <name>n <children>[(mc::MPerson | fc::FPerson)*] ] ->
let tag = match g with "F" -> `woman | "M" -> `man in
let s = map mc with x -> split x in
let d = map fc with x -> split x in
<(tag) name=n>[ <sons>s <daughters>d ]
match argv with
| [ f ] -> load_xml f
| _ -> raise "Invalid command line"
type Person = FPerson | MPerson
type FPerson = <person gender = "F" >[ Name Children ]
type MPerson = <person gender="M">[ Name Children ]
type Children = <children>[Person*]
type Name = <name>[ PCDATA ]
type Man = <man name=String>[ Sons Daughters ]
type Woman = <woman name=String>[ Sons Daughters ]
type Sons = <sons>[ Man* ]
type Daughters = <daughters>[ Woman* ]
let split (MPerson -> Man ; FPerson -> Woman)
<person gender=g>[ <name>n <children>[(mc::MPerson | fc::FPerson)*] ] ->
let tag = match g with "F" -> `woman | "M" -> `man in
let s = map mc with x -> split x in
let d = map fc with x -> split x in
<(tag) name=n>[ <sons>s <daughters>d ]
match argv with
| [ f ] ->
(match load_xml f with
| <doc>(l & [Person*]) -> <doc>(map l with x -> split x)
| _ -> raise "Invalid document")
| _ -> raise "Invalid command line"
import "xml.q"
type Person = FPerson | MPerson
type FPerson = person[ @gender["F"], Name, Children ]
type MPerson = person[ @gender["M"], Name, Children ]
type Children = children[Person*]
type Name = name[ String ]
type Man = man[ @name[String], Sons,Daughters ]
type Woman = woman[ @name[String], Sons,Daughters ]
type Sons = sons[ Man* ]
type Daughters = daughters[ Woman* ]
fun split_children (val c as Person* ) : (Sons,Daughters) =
let val s = filter c {( val m as MPerson { split_m(m) } | FPerson { () })*} in
let val d = filter c {( MPerson { () } | val f as FPerson { split_f(f) })*} in
sons[s], daughters[d]
fun split_m (val p as MPerson) : Man =
match p with
person[ @gender[String], name[val n], children[val c] ] ->
man[ @name[n], split_children(c) ]
fun split_f (val p as FPerson) : Woman =
match p with
person[ @gender[String], name[val n], children[val c] ] ->
woman[ @name[n], split_children(c) ]
fun split_seq (val p as Person* ) : (Man|Woman)* =
filter p {
( val f as FPerson { split_f(f) }
| val m as MPerson { split_m(m) })*
}
let val _ =
match argv() with
val fn as String ->
( match load_xml(fn) with
doc[val p as Person*] -> doc[ split_seq(p) ]
| Any -> raise("Invalid document") )
| Any -> raise("Invalid command line")
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="person[@gender = 'M']">
<man name="{name/text()}">
<sons>
<xsl:apply-templates select="children/person[@gender='M']"/>
</sons>
<daughters>
<xsl:apply-templates select="children/person[@gender='F']"/>
</daughters>
</man>
</xsl:template>
<xsl:template match="person[@gender = 'F']">
<woman name="{name/text()}">
<sons>
<xsl:apply-templates select="children/person[@gender='M']"/>
</sons>
<daughters>
<xsl:apply-templates select="children/person[@gender='F']"/>
</daughters>
</woman>
</xsl:template>
</xsl:stylesheet>
type Person = FPerson | MPerson
type FPerson = <person gender = "F" >[ Name Children ]
type MPerson = <person gender="M">[ Name Children ]
type Children = <children>[Person*]
type Name = <name>[ PCDATA ]
type Man = <man name=String>[ Sons Daughters ]
type Woman = <woman name=String>[ Sons Daughters ]
type Sons = <sons>[ Man* ]
type Daughters = <daughters>[ Woman* ]
let split (MPerson -> Man ; FPerson -> Woman)
<_ gender=g>[ <_>n <_>[(mc::<_ gender="M">_ | fc::_)*] ] ->
let tag = match g with "F" -> `woman | "M" -> `man in
let s = map mc with x -> split x in
let d = map fc with x -> split x in
<(tag) name=n>[ <sons>s <daughters>d ]
match argv with
| [ f ] ->
(match load_xml f with
| <doc>(l & [Person*]) -> <doc>(map l with x -> split x)
| _ -> raise "Invalid document")
| _ -> raise "Invalid command line"
import "xml.q"
type Person = FPerson | MPerson
type MPerson = person[ @gender["M"], Name, Children ]
type FPerson = person[ @gender["F"], Name, Children ]
type Children = children[Person*]
type Name = name[ String ]
type Man = man[ @name[String], Sons,Daughters ]
type Woman = woman[ @name[String], Sons,Daughters ]
type Sons = sons[ Man* ]
type Daughters = daughters[ Woman* ]
fun split_children (val c as Person* ) : (Sons,Daughters) =
let val s = filter c {( val m as ~[ @gender["M"], ~[Any]* ] { split_m(m) } | ~[Any] { () })*} in
let val d = filter c {( val f as ~[ @gender["F"], ~[Any]* ] { split_f(f) } | ~[Any] { () })*} in
sons[s], daughters[d]
fun split_m (val p as MPerson) : Man =
match p with
~[ @gender[String], ~[val n], ~[val c] ] ->
man[ @name[n], split_children(c) ]
fun split_f (val p as FPerson) : Woman =
match p with
~[ @gender[String], ~[val n], ~[val c] ] ->
woman[ @name[n], split_children(c) ]
fun split_seq (val p as Person* ) : (Man|Woman)* =
filter p {
( val f as ~[ @gender["F"], ~[Any], ~[Any] ] { split_f(f) }
| val m as ~[ @gender["M"], ~[Any], ~[Any] ] { split_m(m) })*
}
let val _ =
match argv() with
val fn as String ->
( match load_xml(fn) with
doc[val p as Person*] -> doc[ split_seq(p) ]
| Any -> raise("Invalid document") )
| Any -> raise("Invalid command line")
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="person">
<xsl:variable name="gender">
<xsl:choose>
<xsl:when test="@gender = 'M'">man</xsl:when>
<xsl:otherwise>women</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$gender}">
<xsl:attribute name="name">
<xsl:value-of select="name/text()"/>
</xsl:attribute>
<sons>
<xsl:apply-templates select="children/person[@gender='M']"/>
</sons>
<daughters>
<xsl:apply-templates select="children/person[@gender='F']"/>
</daughters>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment