Ị chere na nhọrọ abụọ a maka ule ọnọdụ n'ime loop bụ otu na arụmọrụ?
if a > b && c*2 > d {
....
}
// и
if a <= b {
continue;
}
if c*2 > d {
....
}
Ihe niile malitere site na "ụbụrụ ụbụrụ"; Ekwesịrị m ịnye ihe atụ nke nchọ kacha mma maka ọnụọgụ ọnụọgụgụ kacha ukwuu n'ụdị ọnụọgụgụ [-x....x]. Anọ m na-eche otú ịrụ ọrụ kacha mma ga-adị ma ọ bụrụ na m na-eji mmụba ezi uche dị na ya site na 1 iji chọpụta ma ọnụọgụgụ ọ dị ọbụna ma ọ bụ na ọ bụghị.
//у четных чисел последний бит всегда равен 0
value & 1 == 0
//vs классический метод
value % 2 == 0
Ahụmịhe mmemme m na Go abụghị nke ukwuu, naanị ihe karịrị otu afọ na ọkara, ejiri m ya, ọ bụ ezie na mgbe mgbe, mana naanị maka ebumnuche bara uru (nke ọma, ma eleghị anya ma ewezuga otu ọrụ metụtara ọrụ http dị elu), yabụ m jiri ya malite. Mepee GoLand wee dee ule dị mfe
package main
import (
"fmt"
"log"
"math"
"math/rand"
"time"
)
const size = 100000000 //math.MaxInt32*2
type Result struct {
Name string
Duration time.Duration
Value int32
}
func main() {
log.Println("initial array capacity: " + fmt.Sprint(size))
var maxValue int32
// Будем варьировать диапазон чисел от минимального
// до максимального. Чем меньше диапазон, тем больше
// процессорного времени будет уходить на операцию
// сравнения текущего числа, с ранее найденным и наоборот
for maxValue = 128; maxValue < math.MaxInt32/2+1; maxValue = maxValue * 2 {
test(maxValue)
}
}
func test(maxValue int32) {
log.Println("max threshold: " + fmt.Sprint(maxValue))
arr := make([]int32, size)
for i := range arr {
arr[i] = rand.Int31n(maxValue)
// в тестовых данных нам нужны и отрицательные числа
sign := rand.Intn(2)
if sign == 1 {
arr[i] = -arr[i]
}
}
// запускаем тест "деление с остатком"
result := maxEvenDividing("maxEvenDividing", arr)
log.Printf(result.Name+"t result: "+fmt.Sprint(result.Value)+"ttduration %s", result.Duration)
// запускаем тест "конъюнкции"
result = maxEvenConjunction("maxEvenConjunction", arr)
log.Printf(result.Name+"t result: "+fmt.Sprint(result.Value)+"ttduration %s", result.Duration)
}
func maxEvenDividing(name string, arr []int32) Result {
start := time.Now()
var current int32 = math.MinInt32
for _, value := range arr {
if value > current && value%2 == 0 {
current = value
}
}
duration := time.Since(start)
result := Result{name, duration, current}
return result
}
func maxEvenConjunction(name string, arr []int32) Result {
start := time.Now()
var current int32 = math.MinInt32
for _, value := range arr {
if value > current && value&1 == 0 {
current = value
}
}
duration := time.Since(start)
result := Result{name, duration, current}
return result
}
Anyị na-enweta nsonaazụ na-egosi na ọnụ ụzọ dị elu, ọtụtụ mgbe mgbanwe na arụmọrụ na-apụta.
Tuleemax threshold: 128
maxEvenDividing result: 126 duration 116.0067ms
maxEvenConjunction result: 126 duration 116.0066ms
max threshold: 16384
maxEvenDividing result: 16382 duration 115.0066ms
maxEvenConjunction result: 16382 duration 111.0064ms
......
max threshold: 8388608
maxEvenDividing result: 8388606 duration 109.0063ms
maxEvenConjunction result: 8388606 duration 109.0062ms
max threshold: 16777216
maxEvenDividing result: 16777214 duration 108.0062ms
maxEvenConjunction result: 16777214 duration 109.0062ms
max threshold: 33554432
maxEvenDividing result: 33554430 duration 114.0066ms
maxEvenConjunction result: 33554430 duration 110.0063ms
max threshold: 67108864
maxEvenDividing result: 67108860 duration 111.0064ms
maxEvenConjunction result: 67108860 duration 109.0062ms
max threshold: 134217728
maxEvenDividing result: 134217726 duration 108.0062ms
maxEvenConjunction result: 134217726 duration 109.0063ms
max threshold: 268435456
maxEvenDividing result: 268435446 duration 111.0063ms
maxEvenConjunction result: 268435446 duration 110.0063ms
O doro anya na n'okwu a, maka ọnụ ụzọ dị iche iche anyị nwere data nyocha dị iche iche, ibu ọrụ nhazi (na laptọọpụ i5-2540M m) dịgasị gburugburu 20..30%, ebe nchekwa nke ngwa na-agba ọsọ site na GoLand nọ na nkezi. ihe dị ka 813MB - nke a na-emetụtakwa ntụkwasị obi nke nsonaazụ ya, ịkwesịrị ịchekwa ikpe ule na diski ma mee ule niile maka ọnụ ụzọ ọ bụla na iche na ibe ya.
Ma ugbu a, na-eche maka otu esi emejuputa ihe a niile na ọnụ ahịa dị ntakịrị, m na-edozi nlele ọnọdụ ozugbo
if value > current && value&1 == 0 {
current = value
}
on
if value <= current {
continue;
}
if value&1 == 0 {
current = value
}
M na-agba ọsọ ule ọzọ ... na m kwụsịrị ịghọta ihe ọ bụla :)
Oge etinyere na ogbugbu na-amalite ịdị iche site na pasentị/irighiri nke pasentị, mana site na 10..15%. M na-agbakwunye ule abụọ ọzọ ngwa ngwa:
func maxEvenDividing2(name string, arr []int32) Result {
start := time.Now()
var current int32 = math.MinInt32
for _, value := range arr {
if value <= current {
continue
}
if value%2 == 0 {
current = value
}
}
duration := time.Since(start)
result := Result{name, duration, current}
return result
}
func maxEvenConjunction2(name string, arr []int32) Result {
start := time.Now()
var current int32 = math.MinInt32
for _, value := range arr {
if value <= current {
continue
}
if value&1 == 0 {
current = value
}
}
duration := time.Since(start)
result := Result{name, duration, current}
return result
}
M na-agba ya ma nweta foto a:mbụ n'usoro ike: 100000000
Oke kachasị: 128
nsonaazụ maxEvenDividing: 126 oge 116.0066ms
nsonaazụ maxEvenDividing2: 126 oge 79.0045ms
nsonaazụ maxEvenConjunction: 126 oge 114.0065ms
nsonaazụ maxEvenConjunction2: 126 oge 83.0048ms
Oke kachasị: 256
nsonaazụ maxEvenDividing: 254 oge 111.0063ms
nsonaazụ maxEvenDividing2: 254 oge 77.0044ms
nsonaazụ maxEvenConjunction: 254 oge 110.0063ms
nsonaazụ maxEvenConjunction2: 254 oge 80.0046ms
Oke kachasị: 512
nsonaazụ maxEvenDividing: 510 oge 114.0066ms
nsonaazụ maxEvenDividing2: 510 oge 80.0045ms
nsonaazụ maxEvenConjunction: 510 oge 110.0063ms
nsonaazụ maxEvenConjunction2: 510 oge 80.0046ms
Oke kachasị: 1024
nsonaazụ maxEvenDividing: 1022 oge 109.0063ms
nsonaazụ maxEvenDividing2: 1022 oge 77.0044ms
nsonaazụ maxEvenConjunction: 1022 oge 111.0063ms
nsonaazụ maxEvenConjunction2: 1022 oge 81.0047ms
Oke kachasị: 2048
nsonaazụ maxEvenDividing: 2046 oge 114.0065ms
nsonaazụ maxEvenDividing2: 2046 oge 79.0045ms
nsonaazụ maxEvenConjunction: 2046 oge 113.0065ms
nsonaazụ maxEvenConjunction2: 2046 oge 81.0046ms
Oke kachasị: 4096
nsonaazụ maxEvenDividing: 4094 oge 114.0065ms
nsonaazụ maxEvenDividing2: 4094 oge 80.0046ms
nsonaazụ maxEvenConjunction: 4094 oge 111.0063ms
nsonaazụ maxEvenConjunction2: 4094 oge 78.0045ms
Oke kachasị: 8192
nsonaazụ maxEvenDividing: 8190 oge 107.0062ms
nsonaazụ maxEvenDividing2: 8190 oge 77.0044ms
nsonaazụ maxEvenConjunction: 8190 oge 111.0063ms
nsonaazụ maxEvenConjunction2: 8190 oge 77.0044ms
Oke kachasị: 16384
nsonaazụ maxEvenDividing: 16382 oge 109.0063ms
nsonaazụ maxEvenDividing2: 16382 oge 77.0044ms
nsonaazụ maxEvenConjunction: 16382 oge 108.0062ms
nsonaazụ maxEvenConjunction2: 16382 oge 77.0044ms
Oke kachasị: 32768
nsonaazụ maxEvenDividing: 32766 oge 112.0064ms
nsonaazụ maxEvenDividing2: 32766 oge 77.0044ms
nsonaazụ maxEvenConjunction: 32766 oge 109.0062ms
nsonaazụ maxEvenConjunction2: 32766 oge 78.0045ms
Oke kachasị: 65536
nsonaazụ maxEvenDividing: 65534 oge 109.0062ms
nsonaazụ maxEvenDividing2: 65534 oge 75.0043ms
nsonaazụ maxEvenConjunction: 65534 oge 109.0063ms
nsonaazụ maxEvenConjunction2: 65534 oge 79.0045ms
Oke kachasị: 131072
nsonaazụ maxEvenDividing: 131070 oge 108.0061ms
nsonaazụ maxEvenDividing2: 131070 oge 76.0044ms
nsonaazụ maxEvenConjunction: 131070 oge 110.0063ms
nsonaazụ maxEvenConjunction2: 131070 oge 80.0046ms
Oke kachasị: 262144
nsonaazụ maxEvenDividing: 262142 oge 110.0063ms
nsonaazụ maxEvenDividing2: 262142 oge 76.0044ms
nsonaazụ maxEvenConjunction: 262142 oge 107.0061ms
nsonaazụ maxEvenConjunction2: 262142 oge 78.0044ms
Oke kachasị: 524288
nsonaazụ maxEvenDividing: 524286 oge 109.0062ms
nsonaazụ maxEvenDividing2: 524286 oge 78.0045ms
nsonaazụ maxEvenConjunction: 524286 oge 109.0062ms
nsonaazụ maxEvenConjunction2: 524286 oge 80.0046ms
Oke kachasị: 1048576
nsonaazụ maxEvenDividing: 1048574 oge 109.0063ms
nsonaazụ maxEvenDividing2: 1048574 oge 80.0045ms
nsonaazụ maxEvenConjunction: 1048574 oge 114.0066ms
nsonaazụ maxEvenConjunction2: 1048574 oge 78.0044ms
Oke kachasị: 2097152
nsonaazụ maxEvenDividing: 2097150 oge 111.0064ms
nsonaazụ maxEvenDividing2: 2097150 oge 79.0045ms
nsonaazụ maxEvenConjunction: 2097150 oge 112.0064ms
nsonaazụ maxEvenConjunction2: 2097150 oge 77.0044ms
Oke kachasị: 4194304
nsonaazụ maxEvenDividing: 4194302 oge 111.0063ms
nsonaazụ maxEvenDividing2: 4194302 oge 78.0045ms
nsonaazụ maxEvenConjunction: 4194302 oge 111.0063ms
nsonaazụ maxEvenConjunction2: 4194302 oge 77.0044ms
Oke kachasị: 8388608
nsonaazụ maxEvenDividing: 8388606 oge 109.0062ms
nsonaazụ maxEvenDividing2: 8388606 oge 78.0045ms
nsonaazụ maxEvenConjunction: 8388606 oge 114.0065ms
nsonaazụ maxEvenConjunction2: 8388606 oge 78.0045ms
Oke kachasị: 16777216
nsonaazụ maxEvenDividing: 16777214 oge 109.0062ms
nsonaazụ maxEvenDividing2: 16777214 oge 77.0044ms
nsonaazụ maxEvenConjunction: 16777214 oge 109.0063ms
nsonaazụ maxEvenConjunction2: 16777214 oge 77.0044ms
Oke kachasị: 33554432
nsonaazụ maxEvenDividing: 33554430 oge 113.0065ms
nsonaazụ maxEvenDividing2: 33554430 oge 78.0045ms
nsonaazụ maxEvenConjunction: 33554430 oge 110.0063ms
nsonaazụ maxEvenConjunction2: 33554430 oge 80.0045ms
Oke kachasị: 67108864
nsonaazụ maxEvenDividing: 67108860 oge 112.0064ms
nsonaazụ maxEvenDividing2: 67108860 oge 77.0044ms
nsonaazụ maxEvenConjunction: 67108860 oge 112.0064ms
nsonaazụ maxEvenConjunction2: 67108860 oge 80.0046ms
Oke kachasị: 134217728
nsonaazụ maxEvenDividing: 134217726 oge 109.0063ms
nsonaazụ maxEvenDividing2: 134217726 oge 78.0044ms
nsonaazụ maxEvenConjunction: 134217726 oge 114.0065ms
nsonaazụ maxEvenConjunction2: 134217726 oge 81.0047ms
Oke kachasị: 268435456
nsonaazụ maxEvenDividing: 268435446 oge 111.0064ms
nsonaazụ maxEvenDividing2: 268435446 oge 79.0045ms
nsonaazụ maxEvenConjunction: 268435446 oge 114.0065ms
nsonaazụ maxEvenConjunction2: 268435446 oge 79.0045ms
Oke kachasị: 536870912
nsonaazụ maxEvenDividing: 536870910 oge 107.0062ms
nsonaazụ maxEvenDividing2: 536870910 oge 76.0043ms
nsonaazụ maxEvenConjunction: 536870910 oge 109.0062ms
nsonaazụ maxEvenConjunction2: 536870910 oge 80.0046ms
Enweghị m ike ịchọta nkọwa doro anya ihe kpatara Go compiler anaghị ebuli koodu ahụ ma na-enyocha ọnọdụ nke abụọ mgbe niile, ọbụlagodi na nke mbụ bụ ụgha. Ma ọ bụ ma eleghị anya anya m na-agba agba ma ahụghị m mmejọ ọ bụla doro anya? Ma ọ bụ na ị chọrọ ịnye ntụziaka pụrụ iche nye onye nchịkọta? Ọ ga-atọ m ụtọ maka nkwupụta ezi uche dị na ya.
PS: Ee, naanị maka ntụrụndụ, agbaara m ule yiri nke ahụ na Java 5 na Java 7/8 - ihe niile doro anya, oge igbu oge bụ otu.
isi: www.habr.com