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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端