| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  | USING: kernel sequences namespaces make math assocs words arrays | 
					
						
							| 
									
										
										
										
											2008-11-23 04:10:21 -05:00
										 |  |  | tools.annotations vocabs sorting prettyprint io system | 
					
						
							| 
									
										
										
										
											2009-03-18 18:01:26 -04:00
										 |  |  | math.statistics accessors tools.time fry ;
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  | IN: wordtimer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: *wordtimes* | 
					
						
							|  |  |  | SYMBOL: *calling* | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : reset-word-timer ( -- )  | 
					
						
							|  |  |  |   H{ } clone *wordtimes* set-global
 | 
					
						
							|  |  |  |   H{ } clone *calling* set-global ;
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | : lookup-word-time ( wordname -- utime n )
 | 
					
						
							|  |  |  |   *wordtimes* get-global [ drop { 0 0 } ] cache first2 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : update-times ( utime current-utime current-numinvokes -- utime' invokes' )
 | 
					
						
							|  |  |  |   rot [ + ] curry [ 1+ ] bi* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : register-time ( utime word -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:32:34 -04:00
										 |  |  |   name>> | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  |   [ lookup-word-time update-times 2array ] keep *wordtimes* get-global set-at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : calling ( word -- )
 | 
					
						
							|  |  |  |   dup *calling* get-global set-at ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : finished ( word -- )
 | 
					
						
							|  |  |  |   *calling* get-global delete-at ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : called-recursively? ( word -- t/f )
 | 
					
						
							|  |  |  |   *calling* get-global at ; inline
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | : timed-call ( quot word -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-18 01:42:12 -05:00
										 |  |  |   [ calling ] [ [ benchmark ] dip register-time ] [ finished ] tri ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : time-unless-recursing ( quot word -- )
 | 
					
						
							|  |  |  |   dup called-recursively? not
 | 
					
						
							|  |  |  |   [ timed-call ] [ drop call ] if ; inline
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | : (add-timer) ( word quot -- quot' )
 | 
					
						
							|  |  |  |   [ swap time-unless-recursing ] 2curry ;  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : add-timer ( word -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-18 18:01:26 -04:00
										 |  |  |   dup '[ [ _ ] dip (add-timer) ] annotate ;
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 14:31:13 -04:00
										 |  |  | : add-timers ( vocab -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  |   words [ add-timer ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 14:31:13 -04:00
										 |  |  | : reset-vocab ( vocab -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  |   words [ reset ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : dummy-word ( -- ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : time-dummy-word ( -- n )
 | 
					
						
							| 
									
										
										
										
											2008-11-23 04:10:21 -05:00
										 |  |  |   [ 100000 [ [ dummy-word ] benchmark , ] times ] { } make median ;
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : subtract-overhead ( {oldtime,n} overhead -- {newtime,n} )
 | 
					
						
							|  |  |  |   [ first2 ] dip
 | 
					
						
							|  |  |  |   swap [ * - ] keep 2array ;
 | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | : change-global ( variable quot -- )
 | 
					
						
							|  |  |  |   global swap change-at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (correct-for-timing-overhead) ( timingshash -- timingshash )
 | 
					
						
							|  |  |  |   time-dummy-word [ subtract-overhead ] curry assoc-map ;   | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : correct-for-timing-overhead ( -- )
 | 
					
						
							|  |  |  |   *wordtimes* [ (correct-for-timing-overhead) ] change-global ;
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | : print-word-timings ( -- )
 | 
					
						
							|  |  |  |   *wordtimes* get-global [ swap suffix ] { } assoc>map natural-sort reverse pprint ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-26 06:31:40 -04:00
										 |  |  | : wordtimer-call ( quot -- )
 | 
					
						
							|  |  |  |   reset-word-timer  | 
					
						
							| 
									
										
										
										
											2008-12-18 01:42:12 -05:00
										 |  |  |   benchmark [ | 
					
						
							|  |  |  |       correct-for-timing-overhead | 
					
						
							|  |  |  |       "total time:" write
 | 
					
						
							|  |  |  |   ] dip pprint nl
 | 
					
						
							| 
									
										
										
										
											2008-07-26 06:31:40 -04:00
										 |  |  |   print-word-timings nl ;
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 14:31:13 -04:00
										 |  |  | : profile-vocab ( vocab quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-08 15:01:28 -04:00
										 |  |  |   "annotating vocab..." print flush
 | 
					
						
							|  |  |  |   over [ reset-vocab ] [ add-timers ] bi
 | 
					
						
							|  |  |  |   reset-word-timer | 
					
						
							|  |  |  |   "executing quotation..." print flush
 | 
					
						
							| 
									
										
										
										
											2008-12-18 01:42:12 -05:00
										 |  |  |   benchmark [ | 
					
						
							|  |  |  |       "resetting annotations..." print flush
 | 
					
						
							|  |  |  |       reset-vocab | 
					
						
							|  |  |  |       correct-for-timing-overhead | 
					
						
							|  |  |  |       "total time:" write
 | 
					
						
							|  |  |  |   ] dip pprint | 
					
						
							| 
									
										
										
										
											2008-08-30 14:31:13 -04:00
										 |  |  |   print-word-timings ;
 |