Guest User

Untitled

a guest
Jun 3rd, 2012
182
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2. import (
  3.     "time"
  4.     "fmt"
  5.     "strings"
  6.     "strconv"
  7. )
  8.  
  9. func pop(stack *[]int) int {
  10.     last := len(*stack) - 1
  11.     ret  := (*stack)[last]
  12.     *stack = (*stack)[:last]
  13.  
  14.     return ret
  15. }
  16.  
  17. func main() {
  18.  
  19.     input := `10 20 100 + - 20 * DUP 10 20 + * =`;
  20.     t := time.Now()
  21.  
  22.     for i := 0; i<10000; i++ {
  23.         stack := []int{}
  24.  
  25.         for _, chunk := range strings.Split(input, " ") {
  26.             switch (chunk) {
  27.             case "+":
  28.                 stack = append(stack, pop(&stack) + pop(&stack))
  29.  
  30.             case "-":
  31.                 stack = append(stack, pop(&stack) - pop(&stack))
  32.  
  33.             case "*":
  34.                 stack = append(stack, pop(&stack) * pop(&stack))
  35.  
  36.             case "=":
  37.                 stack = append(stack, (map[bool]int{true: 1, false: 0})[pop(&stack) == pop(&stack)])
  38.  
  39.             case "DUP":
  40.                 stack = append(stack, stack[len(stack)-1])
  41.  
  42.             case "DUMP":
  43.                 fmt.Println(stack)
  44.  
  45.             default:
  46.                 val, _ := strconv.Atoi(chunk)
  47.                 stack = append(stack, val)
  48.             }
  49.         }
  50.     }
  51.  
  52.     fmt.Printf("Go stack: %s\n", time.Since(t))
  53. }
RAW Paste Data