douxun4173 2012-10-07 19:09 采纳率: 0%
浏览 93
已采纳

在Go中生成长随机字符串的最快方法是什么?

Like [a-zA-Z0-9] string:

na1dopW129T0anN28udaZ

or hexadecimal string:

8c6f78ac23b4a7b8c0182d

By long I mean 2K and more characters.

  • 写回答

6条回答 默认 最新

  • douba4275 2012-10-10 00:26
    关注

    This does about 200MBps on my box. There's obvious room for improvement.

    type randomDataMaker struct {
        src rand.Source
    }
    
    func (r *randomDataMaker) Read(p []byte) (n int, err error) {
        for i := range p {
            p[i] = byte(r.src.Int63() & 0xff)
        }
        return len(p), nil
    }
    

    You'd just use io.CopyN to produce the string you want. Obviously you could adjust the character set on the way in or whatever.

    The nice thing about this model is that it's just an io.Reader so you can use it making anything.

    Test is below:

    func BenchmarkRandomDataMaker(b *testing.B) {
        randomSrc := randomDataMaker{rand.NewSource(1028890720402726901)}
        for i := 0; i < b.N; i++ {
            b.SetBytes(int64(i))
            _, err := io.CopyN(ioutil.Discard, &randomSrc, int64(i))
            if err != nil {
                b.Fatalf("Error copying at %v: %v", i, err)
            }
        }
    }
    

    On one core of my 2.2GHz i7:

    BenchmarkRandomDataMaker       50000        246512 ns/op     202.83 MB/s
    

    EDIT

    Since I wrote the benchmark, I figured I'd do the obvious improvement thing (call out to the random less frequently). With 1/8 the calls to rand, it runs about 4x faster, though it's a big uglier:

    New version:

    func (r *randomDataMaker) Read(p []byte) (n int, err error) {
        todo := len(p)
        offset := 0
        for {
            val := int64(r.src.Int63())
            for i := 0; i < 8; i++ {
                p[offset] = byte(val & 0xff)
                todo--
                if todo == 0 {
                    return len(p), nil
                }
                offset++
                val >>= 8
            }
        }
    
        panic("unreachable")
    }
    

    New benchmark:

    BenchmarkRandomDataMaker      200000        251148 ns/op     796.34 MB/s
    

    EDIT 2

    Took out the masking in the cast to byte since it was redundant. Got a good deal faster:

    BenchmarkRandomDataMaker      200000        231843 ns/op     862.64 MB/s
    

    (this is so much easier than real work sigh)

    EDIT 3

    This came up in irc today, so I released a library. Also, my actual benchmark tool, while useful for relative speed, isn't sufficiently accurate in its reporting.

    I created randbo that you can reuse to produce random streams wherever you may need them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办