| 
            Last change
 on this file since 508671e was             50cfa99, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago           | 
        
        
          | 
             
change Go command-line code to behave like C 
 
           | 
        
        
          
            
              - 
Property                 mode
 set to                 
100644
               
             
           | 
        
        
          | 
            File size:
            798 bytes
           | 
        
      
      
| Line |   | 
|---|
| 1 | package main
 | 
|---|
| 2 | 
 | 
|---|
| 3 | import (
 | 
|---|
| 4 |         "fmt"
 | 
|---|
| 5 |         "time"
 | 
|---|
| 6 |         "os"
 | 
|---|
| 7 |         "strconv"
 | 
|---|
| 8 |         "runtime"
 | 
|---|
| 9 | )
 | 
|---|
| 10 | 
 | 
|---|
| 11 | //=======================================
 | 
|---|
| 12 | // time context switch
 | 
|---|
| 13 | //=======================================
 | 
|---|
| 14 | 
 | 
|---|
| 15 | var shake chan bool = make( chan bool )
 | 
|---|
| 16 | 
 | 
|---|
| 17 | func ContextSwitch(N int) {
 | 
|---|
| 18 |         start := time.Now()
 | 
|---|
| 19 |         for i := 1; i <= N; i += 1 {
 | 
|---|
| 20 |                 runtime.Gosched()
 | 
|---|
| 21 |         }
 | 
|---|
| 22 |         end := time.Now()
 | 
|---|
| 23 |         fmt.Printf("%d\n", end.Sub(start) / time.Duration(N))
 | 
|---|
| 24 |         shake <- true   // indicate completion
 | 
|---|
| 25 | }
 | 
|---|
| 26 | 
 | 
|---|
| 27 | //=======================================
 | 
|---|
| 28 | // benchmark driver
 | 
|---|
| 29 | //=======================================
 | 
|---|
| 30 | 
 | 
|---|
| 31 | func main() {
 | 
|---|
| 32 |         var times int = 10000000
 | 
|---|
| 33 |         if len( os.Args ) > 2 { os.Exit( 1 ) }
 | 
|---|
| 34 |         if len( os.Args ) == 2 { times, _ = strconv.Atoi(os.Args[1]) }
 | 
|---|
| 35 |         go ContextSwitch( times )               // context switch
 | 
|---|
| 36 |         <- shake
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | // Local Variables: //
 | 
|---|
| 40 | // tab-width: 4 //
 | 
|---|
| 41 | // End: //
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.