Skip to main content
deleted 31 characters in body
Source Link
peterSO
  • 3.5k
  • 13
  • 14

In Go, we usually expect reasonable performance and brevity. For example,

package main

import (
    "encoding/hex"
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    b := serial.Bytes()
    buf := make([]byte, 0, 3*len(b))
    x := buf[1*len(b) : 3*len(b)]
    hex.Encode(x, b)
    for i := 0; i < len(x); i += 2 {
        buf = append(buf, x[i], x[i+1], ':')
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         3000000       552 ns/op        72 B/op      3 allocs/op
BenchmarkKissgyorgy       500000      2545 ns/op       120 B/op      7 allocs/op
Benchmark200Success        30000     40675 ns/op     40675 B/op     35 allocs/op

In Go, we usually expect reasonable performance and brevity. For example,

package main

import (
    "encoding/hex"
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    b := serial.Bytes()
    buf := make([]byte, 0, 3*len(b))
    x := buf[1*len(b) : 3*len(b)]
    hex.Encode(x, b)
    for i := 0; i < len(x); i += 2 {
        buf = append(buf, x[i], x[i+1], ':')
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         3000000       552 ns/op        72 B/op      3 allocs/op
BenchmarkKissgyorgy       500000      2545 ns/op       120 B/op      7 allocs/op
Benchmark200Success        30000     40675 ns/op     40675 B/op     35 allocs/op

In Go, we usually expect reasonable performance and brevity. For example,

package main

import (
    "encoding/hex"
    "fmt"
    "math/big"
)

func formatSerial(serial *big.Int) string {
    b := serial.Bytes()
    buf := make([]byte, 0, 3*len(b))
    x := buf[1*len(b) : 3*len(b)]
    hex.Encode(x, b)
    for i := 0; i < len(x); i += 2 {
        buf = append(buf, x[i], x[i+1], ':')
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         3000000       552 ns/op        72 B/op      3 allocs/op
BenchmarkKissgyorgy       500000      2545 ns/op       120 B/op      7 allocs/op
Benchmark200Success        30000     40675 ns/op     40675 B/op     35 allocs/op
improved algorithm
Source Link
peterSO
  • 3.5k
  • 13
  • 14

In Go, we usually expect reasonable performance and brevity. For example,

package main

import (
    "encoding/hex"
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    if serial.Sign() <= 0 {
        return ""
    }
    hexb := serial.TextBytes(16)
    buf := make([]byte, 0, (len3*len(hex)+1b)/2*3)
    ifx len:= buf[1*len(hexb)%2 == 1: {3*len(b)]
        buf = appendhex.Encode(bufx, '0'b)
    }
    for i := 0; i < len(hexx); i++i += 2 {
        buf = append(buf, hex[i])
        if (len(buf)+1)%3 == 0 {
            buf =x[i], append(bufx[i+1], ':')
        }
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         50000003000000       245552 ns/op        9672 B/op    4  3 allocs/op
BenchmarkKissgyorgy      2000000 500000      6942545 ns/op       120 B/op      7 allocs/op
Benchmark200Success       200000 30000    11614 40675 ns/op     40675 B/op     35 allocs/op

In Go, we usually expect reasonable performance. For example,

package main

import (
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    if serial.Sign() <= 0 {
        return ""
    }
    hex := serial.Text(16)
    buf := make([]byte, 0, (len(hex)+1)/2*3)
    if len(hex)%2 == 1 {
        buf = append(buf, '0')
    }
    for i := 0; i < len(hex); i++ {
        buf = append(buf, hex[i])
        if (len(buf)+1)%3 == 0 {
            buf = append(buf, ':')
        }
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         5000000       245 ns/op        96 B/op    4 allocs/op
BenchmarkKissgyorgy      2000000       694 ns/op       120 B/op    7 allocs/op
Benchmark200Success       200000     11614 ns/op     40675 B/op   35 allocs/op

In Go, we usually expect reasonable performance and brevity. For example,

package main

import (
    "encoding/hex"
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    b := serial.Bytes()
    buf := make([]byte, 0, 3*len(b))
    x := buf[1*len(b) : 3*len(b)]
    hex.Encode(x, b)
    for i := 0; i < len(x); i += 2 {
        buf = append(buf, x[i], x[i+1], ':')
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         3000000       552 ns/op        72 B/op      3 allocs/op
BenchmarkKissgyorgy       500000      2545 ns/op       120 B/op      7 allocs/op
Benchmark200Success        30000     40675 ns/op     40675 B/op     35 allocs/op
deleted 7 characters in body
Source Link
peterSO
  • 3.5k
  • 13
  • 14

In Go, we usually expect reasonable performance. For example,

package main

import (
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    if serial.Sign() <= 0 {
        return ""
    }
    hex := serial.Text(16)
    buf := make([]byte, 0, (len(hex)+1)/2*3)
    j := 0
    if len(hex)%2 == 1 {
        buf[0]buf = '0'
       append(buf, j++'0')
    }
    for i := 0; i < len(hex); i++ {
        buf[j]buf = hex[i]
       append(buf, j++hex[i])
        if (j+1len(buf)+1)%3 == 0 {
            buf[j]buf = append(buf, ':'
            j++)
        }
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         5000000       242245 ns/op        96 B/op    4 allocs/op
BenchmarkKissgyorgy      2000000       694 ns/op       120 B/op    7 allocs/op
Benchmark200Success       200000     11614 ns/op     40675 B/op   35 allocs/op

In Go, we usually expect reasonable performance. For example,

package main

import (
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    if serial.Sign() <= 0 {
        return ""
    }
    hex := serial.Text(16)
    buf := make([]byte, (len(hex)+1)/2*3)
    j := 0
    if len(hex)%2 == 1 {
        buf[0] = '0'
        j++
    }
    for i := 0; i < len(hex); i++ {
        buf[j] = hex[i]
        j++
        if (j+1)%3 == 0 {
            buf[j] = ':'
            j++
        }
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         5000000       242 ns/op        96 B/op    4 allocs/op
BenchmarkKissgyorgy      2000000       694 ns/op       120 B/op    7 allocs/op
Benchmark200Success       200000     11614 ns/op     40675 B/op   35 allocs/op

In Go, we usually expect reasonable performance. For example,

package main

import (
    "fmt"
    "math/big"
    "regexp"
    "strings"
)

func formatSerial(serial *big.Int) string {
    if serial.Sign() <= 0 {
        return ""
    }
    hex := serial.Text(16)
    buf := make([]byte, 0, (len(hex)+1)/2*3)
    if len(hex)%2 == 1 {
        buf = append(buf, '0')
    }
    for i := 0; i < len(hex); i++ {
        buf = append(buf, hex[i])
        if (len(buf)+1)%3 == 0 {
            buf = append(buf, ':')
        }
    }
    return string(buf[:len(buf)-1])
}

func main() {
    fmt.Println(formatSerial(big.NewInt(1234)))
    // "04:d2"
    fmt.Println(formatSerial(big.NewInt(123456)))
    // "01:e2:40"
    fmt.Println(formatSerial(big.NewInt(1234567891011121314)))
    // "11:22:10:f4:b2:d2:30:a2"
}

Output:

04:d2
01:e2:40
11:22:10:f4:b2:d2:30:a2

Benchmark: serial := big.NewInt(1234567891011121314):

BenchmarkPeterSO         5000000       245 ns/op        96 B/op    4 allocs/op
BenchmarkKissgyorgy      2000000       694 ns/op       120 B/op    7 allocs/op
Benchmark200Success       200000     11614 ns/op     40675 B/op   35 allocs/op
edited body
Source Link
peterSO
  • 3.5k
  • 13
  • 14
Loading
Source Link
peterSO
  • 3.5k
  • 13
  • 14
Loading