pro speedtest
; This is designed to demonstrate factors that effect speed of execution.
; 
; Things that slow down a program :
; 1. Using data types that are more complex than needed.
; 2. Using loops instead of built-in array operations
; 3. Allocating memory for new variables or increasing the size of old variables.
; 4. Hard disk access: read/write operations (virtual memory included).

; TO RUN THIS PROGRAM:
; a. compile and run it.
; b. You will be asked to make a selection.  The selections are grouped so that
;    the first digit represents the number of the category above; the second digit
;    represents the example within that category.  '23' refers to the 3rd example in
;    the 'using loops' category.
; c. to exit, enter a '0' for your selection.

; Make a big loop to run different examples as requested.

select = 1

while (select ne 0) do begin

print,'enter the number of your example'
read, select

  case select of

     	;----------------------------------------------
        ; cases 1x correspond to using improper data types
	;----------------------------------------------

         0: ; exit the loop

	11: begin  ; byte addition
	    
              A = make_array(1000,1000,value=1,/byte)
              B = make_array(1000,1000,value=2,/byte)

              time1 = systime(/seconds)
              C = A+B
              time2 = systime(/seconds)

              print, 'time = ',time2-time1

            end

	12: begin  ;integer addition

 	      A = make_array(1000,1000,value=1,/integer)
              B = make_array(1000,1000,value=2,/integer)

              time1 = systime(/seconds)
              C = A+B
              time2 = systime(/seconds)

              print, 'time = ',time2-time1

            end

	13: begin   ; float addition

 	      A = make_array(1000,1000,value=1,/float)
              B = make_array(1000,1000,value=2,/float)

              time1 = systime(/seconds)
              C = A+B
              time2 = systime(/seconds)

              print, 'time = ',time2-time1

            end

	14: begin   ; byte multiplication
	    
              A = make_array(1000,1000,value=1,/byte)
              B = make_array(1000,1000,value=2,/byte)

              time1 = systime(/seconds)
              C = A*B
              time2 = systime(/seconds)

              print, 'time = ',time2-time1

            end

	15: begin   ; integer multiplication

 	      A = make_array(1000,1000,value=1,/integer)
              B = make_array(1000,1000,value=2,/integer)

              time1 = systime(/seconds)
              C = A*B
              time2 = systime(/seconds)

              print, 'time = ',time2-time1

            end

	16: begin  ; float multiplication

 	      A = make_array(1000,1000,value=1,/float)
              B = make_array(1000,1000,value=2,/float)

              time1 = systime(/seconds)
              C = A*B
              time2 = systime(/seconds)

              print, 'time = ',time2-time1

            end

           ;----------------------------------------       
           ; cases 2x -loop instead of array operation
	   ;----------------------------------------

  	21: begin   ; array addition

              A = make_array(1000,1000,value=1,/integer)
              B = make_array(1000,1000,value=2,/integer)
              C = intarr(1000,1000)

	       time1 = systime(/seconds)

              for y = 0, 999 do begin
                for x = 0, 999 do begin

                  C[x,y] = A[x,y]+B[x,y]

                endfor
               endfor

               time2 = systime(/seconds)

               print,'time = ',time2-time1

           end  

	22: begin 

 	      A = make_array(1000,1000,value=1,/integer)
              B = make_array(1000,1000,value=2,/integer)
              C = intarr(1000,1000)

	       time1 = systime(/seconds)

               C = A+B

               time2 = systime(/seconds)

               print,'time = ',time2-time1

           end 

	23: begin   ; searching using a loop 

              A = findgen(1000)
              searchvalue = 470

              time1 = systime(/seconds)
              for i = 0,999 do if (A[i] eq searchvalue) then index = i
              time2 = systime(/seconds)

              print,'time=',time2-time1

              print,'index=',index

            end

	24: begin   ; searching using the 'where' function 

              A = findgen(1000)
              searchvalue = 470

              time1 = systime(/seconds)
              index = where(A eq searchvalue)
              time2 = systime(/seconds)

              print,'time=',time2-time1

              print,'index=',index[0]

            end

	;-------------------------------------
        ; cases 3x: memory allocation slows a program 
	;-------------------------------------        
       
	31: begin  ; change variable size inside a loop
    
              time1 = systime(/seconds)              

              A = [0]
              for i = 1,999 do A = [A,i]
              
              time2 = systime(/seconds)

              print,'time =',time2-time1
 
             end

	32: begin  ; allocate all memory before the loop

		time1 = systime(/seconds)

		A = intarr(1000)
		for i = 1,999 do A[i] = i

                time2 = systime(/seconds)

		print,'time =',time2-time1     

    	     end    

	33: begin  ; introduce extra variables into memory

		A = findgen(1000)  ;  1.0,2.0,3.0 etc  

                time1 = systime(/seconds)
 
                A2 = 2*A   

                time2 = systime(/seconds)    

                print,'time = ',time2-time1

		A2 = 0 ; to decrease space in memory

             end

	34: begin  ; write over memory of old variable 

		A = findgen(1000)  ;  1.0,2.0,3.0 etc 	

                time1 = systime(/seconds)
  
                A = 2*A   

                time2 = systime(/seconds)    

                print,'time = ',time2-time1   

	     end  

	;---------------------------------------
	; cases 4x: reading/writing to disk is slow
	;---------------------------------------
	           
	41: begin  ; write data to a file bit by bit 	

                openw,1,'test.dat'
                size = 1000

                time1 = systime(/seconds)

                for i = 0,size-1 do printf,1, i                  

                time2 = systime(/seconds)  
                close,1  

                print,'time = ',time2-time1   

	     end  
	            
	           
	42: begin  ; write data to a file all at once	

                openw,1,'test.dat'
                size = 1000

                time1 = systime(/seconds)
                
                A = intarr(size)

                for i = 0,size-1 do A[i] = i

                printf,1,A                  

                time2 = systime(/seconds)    
                close,1

                print,'time = ',time2-time1   

	     end  

	43: begin ; writing and reading a file in ASCII

             size = 1000
             A = findgen(size,size)  ; data
             B = fltarr(size,size)   ; empty variable (full of zeroes)
             openw,1,'test.dat'

            time1 = systime(/seconds)

            ;write data to file
             printf,1, A

            ; close file and open to read
             close,1
             openr,1,'test.dat'

            ; read data into empty variable
              readf,1,B

            time2 = systime(/seconds)
            close,1

            print,'formatted write/read time =',time2-time1

         end

	44: begin ; writing and reading a file in binary

             size = 1000
             A = findgen(size,size)  ; data
             B = fltarr(size,size)   ; empty variable (full of zeroes)
             openw,1,'test.dat'

            time1 = systime(/seconds)

            ;write data to file
             writeu,1, A

            ; close file and open to read
             close,1
             openr,1,'test.dat'

            ; read data into empty variable
              readu,1,B

            time2 = systime(/seconds)
            close,1

            print,'unformatted write/read time =',time2-time1

         end

    endcase

endwhile

end