namespace path {::tcl::mathop}

source benchmark_unrolled.tcl

set ::RADIAN_CONV  57.2957795131

proc distance { lat1 lon1 lat2 lon2 } {
	set lat1 [expr {$lat1 / $::RADIAN_CONV}]
	set lat2 [expr {$lat2 / $::RADIAN_CONV}]
	set dlon [expr {($lon2 - $lon1) / $::RADIAN_CONV}]

	return [expr {acos(sin($lat1) * sin($lat2) + \
				cos($lat1) * cos($lat2) * cos($dlon)) * \
			6371.0}]
}

proc distance2 {lat1 lon1 lat2 lon2} {
	global RADIAN_CONV
	set lat1 [/ $lat1 $RADIAN_CONV]
	set lat2 [/ $lat2 $RADIAN_CONV]
	set dlon [expr {($lon2 - $lon1) / $RADIAN_CONV}]

	expr {acos(sin($lat1) * sin($lat2) + \
				cos($lat1) * cos($lat2) * cos($dlon)) * \
			6371.0}
}

proc distance3 {lat1 lon1 lat2 lon2} {
	set lat1 [/ $lat1 57.2957795131]
	set lat2 [/ $lat2 57.2957795131]

	expr {
		acos(sin($lat1) * sin($lat2) +
		     cos($lat1) * cos($lat2) * cos(($lon2 - $lon1) / 57.2957795131)
		) * 6371.0
	}
}

puts "Tcl version [info patchlevel]"
puts "distance  [distance 41.0 -81.0 42.0 -82.0]"
puts "distance2 [distance2 41.0 -81.0 42.0 -82.0]"
puts "distance3 [distance3 41.0 -81.0 42.0 -82.0]"

foreach unroll {1 2 5 8 10} {
	puts "\n\n\n=x= unroll: $unroll =x="
	benchmark::test 1000 1000 $unroll usec distance 41.0 -81.0 42.0 -82.0
	benchmark::test 1000 1000 $unroll usec distance2 41.0 -81.0 42.0 -82.0
	benchmark::test 1000 1000 $unroll usec distance3 41.0 -81.0 42.0 -82.0
}
