;	Time test procedure.. Print values of commonly used timings.
subr timer, name	;Print timing information, name = descriptive
;	string for message.


t = !systime		;Get current time.
#ntest = #ntest + 1
tt = t - #time
#total_time = #total_time + tt
#geom_time = #geom_time + alog(tt)
print, #ntest, float(tt), ' ',name
#time = t
endsubr

subr init_time,file	;Initialize timer, file = optional param
;	containing name of file to write info to

#total_time = 0.
#geom_time = 0.
#ntest = 0
#time = !systime
return
end



subr reset,dummy	;Reset timer, used to ignore set up times...
; No-op this procedure to include setup times mainly comprise
; the time required to allocate arrays and to set them to
; a given value.

#time = !systime
return
end

subr dummy,dummy
return
end



subr graphics_times, filename
; Time common graphics operations in the same manner as time_test

if n_params() gt 0 then init_time,filename else init_time

for i=1,10 do {
	plot,[0,1]+i
	empty
}
timer,'Simple plot, 10 times'

n = 1000
x = randomu(n) * #2pi)
y = fix((sin(x)+1) * (0.5 * #d.y_vsize))
x = fix((cos(x)+1) * (0.5 * #d.x_vsize))
for i=1,5 do {
	erase
	plots,x,y
}
timer,'vectors'
 
n = 24
plot,[-1,1],[-1,1]

for i=3,n do {
	x = indgen(fltarr(i)) * ( #2pi / i)
	erase
	polyfill,sin(x),cos(x)
	empty
}
timer,'Polygon filling'

n = 512
a = indgen(fltarr(n)) * (8 * #pi / n)
;c = bytscl(sin(a) # cos(a), top = #d.n_colors-1)
;d = not c
erase
reset
for i=1,5 do {
	tv,c
	empty
	tv,d
	empty
}
timer,'Display 512 x 512 image, 10 times'
;for i=1,10 do begin
;	c = 0
;	c = tvrd(0,0,512,512)
;	endfor
;timer,'Read back 512 by 512 image, 10 times'

print, float(#total_time),' Total Time'
print, exp(#geom_time / #ntest), ' Geometric mean,',#ntest,' tests.'
endsubr



subr time_test, filename		;Run some time tests.
; Machine / OS / Memory            Comp.   Geo. Avg.   Graphics Geo. Avg.
;
; MicroVAX II, VMS 5.1, 16MB        637     14.4        39.9    6.57
; MicroVAX II, Ultrix 3.0, 16MB     616     13.9        58.1    8.27
; Sun 3/110, SunOS 4.0, 12MB        391      8.19       32.0    7.81
; Sun 3/80, 12MB, 24 bit color      282      6.03       89.3   21.7
; PC 386 25MHz, 80387, MSDOS, 4MB   276      6.9        29.5    5.94
; Mips R2030, RISC/os 4.1, 8MB      246      3.67       14.6    2.62
; VAXStation 3100, VMS 5.1, 16MB    235      5.13       24.3    3.71
; HP 9000, Model 375, ?? ??         163      4.14       20.8    3.37
; DecStation 3100, UWS 2.1, 16MB    150      4.00       17.6    3.23
; 486 33mhz Clone, MS Windows, 8MB   70      1.81       12.9    3.00
; Sun 4/65, SunOS 4.1, 16MB          66      1.81        7.0    1.64
; Silicon Graphics 4D/25, ??         51      1.38       19.4    2.44
; Sun 4/50 IPX, 16MB		     40	     1.03	 7.7	0.80
; IBM 6000 Model 325 24MB            40      0.87        5.8    1.21
; HP 9000 / 700 32 MB                24      0.63        8.2    1.62
;


do_floating = 1	;Do floating point array tests


if !narg gt 0 then init_time,filename else init_time

;	almost Empty for loop
;for i=0, 999999 do { }

timer,'ANA doesn''t execute empty loops'
for i=1,100000 do dummy, i
timer,'Call empty procedure (1 param) 100,000 times'

;	Add 100000 scalar ints:...
for i=0,99999 do a=i+1
timer,'Add 100,000 integer scalars and store'

;	Scalar arithmetic loop:
for i=0,25000 do begin
	a = i + i -2
	b = a / 2 + 1
	if b ne i then type,'You screwed up',i,a,b
	end
	
timer,'25,000 scalar loops each of 5 ops, 2 =, 1 if)'

a=zero(bytarr(512,512))+byte(2)
reset
for i=1,10 do b=a*byte(2)
timer,'Mult 512 by 512 byte by constant and store, 10 times'
;for i=1,10 do c = shift(b,10,10)
;timer,'Shift 512 by 512 byte and store, 10 times'
for i=1,10 do b=a+byte(3)
timer,'Add constant to 512 x 512 byte array and store, 10 times'
for i=1,10 do b=a+b
timer,'Add two 512 by 512 byte images and store, 10 times'

if do_floating then {
	a = float(a)
	reset
	for i=1,10 do b=a*byte(2)
	timer,'Mult 512 by 512 floating by constant and store, 10 times'
	for i=1,10 do b=a+byte(2)	
	timer,'Add constant to 512 x 512 floating and store, 10 times'
	for i=1,10 do b=a+b
	timer,'Add two 512 by 512 floating images and store, 10 times'
}


a=randomu(100,100)	;Random number matrix
reset
b = randomu(100)
decomp,a
dsolve,a,b
timer,'solve a 100 by 100 linear system'

a=indgen(bytarr(256,256)) & b=a
reset
for i=0,255 do for j=0,255 do b(j,i)=a(i,j)
timer,'Transpose 256 x 256 byte, FOR loops'
for i=0,255 do begin
	b(0,i) = a(i,*)
	end
timer,'Transpose 256 x 256 byte, row and column ops'
b=a(>1,>0)
timer,'Transpose 256 x 256 byte, transpose function'

a=indgen(fltarr(100000))+1
c=a
b = a
reset
for i=0,num_elem(a)-1 do b(i) = alog(a(i))
timer,'Log of 100,000 numbers, FOR loop'
b = alog(a)
timer,'Log of 100,000 numbers, vector ops'

for i=0,num_elem(a)-1 do c(i)=a(i)+b(i)
timer,'Add two 100000 element floating vectors, FOR loop'

c=a+b
timer,'Add two 100000 element floating vectors, vector op'

;a = indgen(fltarr(65536))
;reset
;b=fft(a,1)
;timer,'65536 point real to complex FFT'

;a=bytarr(512,512)
;a(200:250,200:250)=byte(10)
;reset
;b=smooth(a,5)
;timer,'Smooth 512 by 512 byte array, 5x5 boxcar'

;a=float(a)
;reset
;b=smooth(a,5)
;timer,'Smooth 512 by 512 floating array, 5x5 boxcar'

a=indgen(bytarr(512,512)
aa =assoc(2,a)
reset
openw,2,'test.dat'
for i=1,10 do aa(0) = a
close,2
openr,2,'test.dat'
for i=1,10 do a=aa(0)
timer,'Write and read 10 512 by 512 byte arrays'
close,2

print, float(#total_time),' Total Time'
print, exp(#geom_time / #ntest), ' Geometric mean,',#ntest,' tests.'
;  Remove the data file
spawn,'rm test.dat'
end
