# -*- tcl -*-
# Tcl Benchmark File
#
# This file contains a number of benchmarks for the 'struct::tree'
# data structure to allow developers to monitor package performance.
#
# (c) 2003 Andreas Kupries <andreas_kupries@users.sourceforge.net>

package require  struct
namespace import struct::*

bench -desc "tree create" -body {set t [tree]} -post {$t destroy}

proc foo {} {proc foo {} {$::t destroy}}
bench -desc "tree destroy 0" -pre {set t [tree]} -body {foo} -iter 1

foreach n {1 10 100 1000} {
    proc foo {} {proc foo {} {$::t destroy}}
    bench -desc "tree destroy $n" -pre {
	set t [tree]
	for {set i 0} {$i < $n} {incr i} {$t insert root 0}
    } -body {foo} -iter 1
}

foreach it {1 10 100 1000} {
    bench -desc "tree insert root end (x$it)" \
	    -pre {set t [tree]} -post {$t destroy} \
	    -body {$t insert root end} -iter $it

    bench -desc "tree insert root 0 (x$it)" \
	    -pre {set t [tree]} -post {$t destroy} \
	    -body {$t insert root 0} -iter $it

    bench -desc "tree insert root 5 (x$it)" -pre {
	set t [tree]
	for {set i 0} {$i < 10} {incr i} {$t insert root end}
    } -post {$t destroy} -body {$t insert root 5} -iter $it ; # {}

    bench -desc "tree insert root end-5 (x$it)" -pre {
	set t [tree]
	for {set i 0} {$i < 10} {incr i} {$t insert root end}
    } -post {$t destroy} -body {$t insert root end-5} -iter $it ; # {}
}

foreach n {1 10 100 1000} {
    bench -desc "tree children $n" -pre {
	set t [tree]
	for {set i 0} {$i < $n} {incr i} {$t insert root 0}
    } -body {$t children root} -post {$t destroy}

    bench -desc "tree numchildren root $n" -pre {
	set t [tree]
	for {set i 0} {$i < $n} {incr i} {$t insert root 0}
    } -body {$t numchildren root} -post {$t destroy}

    bench -desc "tree depth root $n" -pre {
	set t [tree] ; set p root
	for {set i 0} {$i < $n} {incr i} {set p [$t insert $p 0]}
    } -body {$t depth root} -post {$t destroy}

    bench -desc "tree isleaf root $n" -pre {
	set t [tree] ; set p root
	for {set i 0} {$i < $n} {incr i} {set p [$t insert $p 0]}
    } -body {$t isleaf root} -post {$t destroy}

    bench -desc "tree isleaf bott $n" -pre {
	set t [tree] ; set p root
	for {set i 0} {$i < $n} {incr i} {set p [$t insert $p 0]}
    } -body {$t isleaf $p} -post {$t destroy}

    bench -desc "tree numchildren bott $n" -pre {
	set t [tree]
	for {set i 0} {$i < $n} {incr i} {$t insert root 0}
    } -body {$t numchildren $p} -post {$t destroy}

    bench -desc "tree parent bott $n" -pre {
	set t [tree]
	for {set i 0} {$i < $n} {incr i} {$t insert root 0}
    } -body {$t parent $p} -post {$t destroy}

    bench -desc "tree parent root $n" -pre {
	set t [tree]
	for {set i 0} {$i < $n} {incr i} {$t insert root 0}
    } -body {$t parent $p} -post {$t destroy}
}

bench -desc "tree exists root" \
	-pre {set t [tree]} -post {$t destroy} \
	-body {$t exists root}

bench -desc "tree exists miss" \
	-pre {set t [tree]} -post {$t destroy} \
	-body {$t exists miss}
