Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This does about 200MBps on my box. There's obvious room for improvement.</p> <pre><code>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() &amp; 0xff) } return len(p), nil } </code></pre> <p>You'd just use <code>io.CopyN</code> to produce the string you want. Obviously you could adjust the character set on the way in or whatever.</p> <p>The nice thing about this model is that it's just an <code>io.Reader</code> so you can use it making anything.</p> <p>Test is below:</p> <pre><code>func BenchmarkRandomDataMaker(b *testing.B) { randomSrc := randomDataMaker{rand.NewSource(1028890720402726901)} for i := 0; i &lt; b.N; i++ { b.SetBytes(int64(i)) _, err := io.CopyN(ioutil.Discard, &amp;randomSrc, int64(i)) if err != nil { b.Fatalf("Error copying at %v: %v", i, err) } } } </code></pre> <p>On one core of my 2.2GHz i7:</p> <pre><code>BenchmarkRandomDataMaker 50000 246512 ns/op 202.83 MB/s </code></pre> <p><strong>EDIT</strong></p> <p>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:</p> <p>New version:</p> <pre><code>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 &lt; 8; i++ { p[offset] = byte(val &amp; 0xff) todo-- if todo == 0 { return len(p), nil } offset++ val &gt;&gt;= 8 } } panic("unreachable") } </code></pre> <p>New benchmark:</p> <pre><code>BenchmarkRandomDataMaker 200000 251148 ns/op 796.34 MB/s </code></pre> <p><strong>EDIT 2</strong></p> <p>Took out the masking in the cast to byte since it was redundant. Got a good deal faster:</p> <pre><code>BenchmarkRandomDataMaker 200000 231843 ns/op 862.64 MB/s </code></pre> <p>(this is so much easier than real work <em>sigh</em>)</p> <p><strong>EDIT 3</strong></p> <p>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.</p> <p>I created <a href="https://github.com/dustin/randbo">randbo</a> that you can reuse to produce random streams wherever you may need them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload