golang test array slice map

func testTime()  {
   loopNum := 100000*10000

   go func(loopNum int) {
      arr := [size]int{1,2,3,4,5}
      for i:=0;i<len(arr);i++{
         arr[i]=i
      }
      tOld := time.Now()
      for i:=0;i<loopNum;i++{
         value := arr[i%size]
         if false{
            fmt.Printf("%d,%d\n",loopNum,value)
         }
      }
      tNew := time.Now()
      tMin := tNew.Minute()-tOld.Minute()
      tSed := tNew.Second()-tOld.Second()
      tSed = tMin*60+tSed
      fmt.Printf("test arr    size:%d cost time:%d\n",len(arr),tSed*1000*1000*1000+tNew.Nanosecond()-tOld.Nanosecond())
   }(loopNum)

   go func(loopNum int) {
      arr := [size]int{1,2,3,4,5}
      for i:=0;i<size;i++{
         arr[i]=i
      }
      slice := arr[:]
      tOld := time.Now()
      for i:=0;i<loopNum;i++{
         value := slice[i%size]
         if false{
            fmt.Printf("%d,%d\n",loopNum,value)
         }
      }
      tNew := time.Now()
      tMin := tNew.Minute()-tOld.Minute()
      tSed := tNew.Second()-tOld.Second()
      tSed = tMin*60+tSed
      fmt.Printf("test slice  size:%d cost time:%d\n",len(slice),tSed*1000*1000*1000+tNew.Nanosecond()-tOld.Nanosecond())
   }(loopNum)

   go func(loopNum int) {
       map2  := make(map[int]int)

      for i:=0;i<size;i++{
         map2[i]=i
      }
      tOld := time.Now()
      for i:=0;i<loopNum;i++{
         value := map2[i%size]
         if false{
            fmt.Printf("%d,%d\n",loopNum,value)
         }
      }
      tNew := time.Now()
      tMin := tNew.Minute()-tOld.Minute()
      tSed := tNew.Second()-tOld.Second()
      tSed = tMin*60+tSed
      fmt.Printf("test map2   size:%d cost time:%d\n",len(map2),tSed*1000*1000*1000+tNew.Nanosecond()-tOld.Nanosecond())
   }(loopNum)
}

size=10

test arr       size:10 cost time:1281287900
test slice     size:10 cost time:1282287900
test map2   size:10 cost time:9804217200

size=100

test slice  size:100 cost time:1233275300
test arr    size:100 cost time:1236276000
test map2   size:100 cost time:16125632500
 

size=1000

test arr        size:1000 cost time:1131253000
test slice     size:1000 cost time:1140255100
test map2   size:1000 cost time:20600644200

size = 10000

test slice  size:10000 cost time:1120241600
test arr    size:10000 cost time:1133255100
test map2   size:10000 cost time:25400722700

size = 10*10000

test slice     size:100000 cost time:1143812300
test arr        size:100000 cost time:1143812300
test map2   size:100000 cost time:30350851200

size = 100*10000

test slice     size:1000000 cost time:1126461400
test arr        size:1000000 cost time:1126461400
test map2   size:1000000 cost time:72473326100

size = 1000*10000

test slice     size:10000000 cost time:1251294200
test arr        size:10000000 cost time:1262296900
test map2   size:10000000 cost time:86622657300

size = 5000*10000

test arr    size:50000000     cost time:1238287400
test slice  size:50000000    cost time:1235287400
test map2   size:50000000 cost time:106376998500

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值