Created
February 16, 2018 11:12
-
-
Save mrLSD/7d54e79692cc2b7567051e9e4a4f3a0b to your computer and use it in GitHub Desktop.
concurent map copy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "sync" | |
| "time" | |
| ) | |
| var m = make(map[string]int32) | |
| var wg = sync.WaitGroup{} | |
| var mutex = sync.Mutex{} | |
| func tst() { | |
| defer wg.Done() | |
| println("RUN") | |
| m1 := make(map[string]int32) | |
| t := time.Now() | |
| mutex.Lock() | |
| for k, v := range m { | |
| m1[k] = v | |
| } | |
| mutex.Unlock() | |
| fmt.Printf("DONE (%d): %+v\n", len(m1), time.Now().Sub(t).Seconds()) | |
| } | |
| func main() { | |
| t := time.Now() | |
| var i int32 | |
| for i = 0; i < 10000; i++ { | |
| key := fmt.Sprintf("key%d", i) | |
| m[key] = i | |
| } | |
| fmt.Printf("DONE (%d): %+v\n", len(m), time.Now().Sub(t).Seconds()) | |
| for i = 0; i < 2; i++ { | |
| wg.Add(1) | |
| go tst() | |
| } | |
| wg.Wait() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment