Created
June 25, 2019 08:52
-
-
Save chikien276/d494587b2f8fedefcf68382d52d43198 to your computer and use it in GitHub Desktop.
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" | |
"math/rand" | |
"strings" | |
"time" | |
) | |
func main() { | |
rand.Seed(int64(time.Now().Nanosecond())) | |
var arr []string | |
for i := 0; i < 1000000; i++ { | |
strLen := rand.Int() % 10 | |
arr = append(arr, strings.Repeat("a", strLen)) | |
} | |
start := time.Now() | |
for i := 0; i < 1000000-4; i++ { | |
checkStrLenMath(arr[i], arr[i+1], arr[i+2], arr[i+3]) | |
} | |
elapsed := time.Since(start) | |
fmt.Printf("checkStrLenMath took %d \n", elapsed) | |
start = time.Now() | |
for i := 0; i < 1000000-4; i++ { | |
checkStrLen(arr[i], arr[i+1], arr[i+2], arr[i+3]) | |
} | |
elapsed = time.Since(start) | |
fmt.Printf("checkStrLen took %d\n", elapsed) | |
start = time.Now() | |
for i := 0; i < 1000000-4; i++ { | |
checkStrLenX(arr[i], arr[i+1], arr[i+2], arr[i+3]) | |
} | |
elapsed = time.Since(start) | |
fmt.Printf("checkStrLenX took %d\n", elapsed) | |
} | |
func checkStrLenMath(as, bs, cs, ds string) bool { | |
a := len(as) | |
b := len(bs) | |
c := len(cs) | |
d := len(ds) | |
return (a+b+c+d == 0) || (a*b*c*d != 0) | |
} | |
func checkStrLen(as, bs, cs, ds string) bool { | |
a := len(as) | |
b := len(bs) | |
c := len(cs) | |
d := len(ds) | |
return (a == 0 && b == 0 && c == 0 && d == 0) || (a != 0 && b != 0 && c != 0 && d != 0) | |
} | |
func checkStrLenX(as, bs, cs, ds string) bool { | |
return (len(as) == 0 && len(bs) == 0 && len(cs) == 0 && | |
len(ds) == 0) || (len(as) != 0 && len(bs) != 0 && len(cs) != 0 && len(ds) != 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment