Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: base/rand_util.cc

Issue 140773006: Improve base::RandBytes() performance by 1.75x-2.10x on POSIX. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/rand_util_nacl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/rand_util.h" 5 #include "base/rand_util.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits> 10 #include <limits>
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 (std::numeric_limits<uint64>::max() / range) * range - 1; 54 (std::numeric_limits<uint64>::max() / range) * range - 1;
55 55
56 uint64 value; 56 uint64 value;
57 do { 57 do {
58 value = base::RandUint64(); 58 value = base::RandUint64();
59 } while (value > max_acceptable_value); 59 } while (value > max_acceptable_value);
60 60
61 return value % range; 61 return value % range;
62 } 62 }
63 63
64 void RandBytes(void* output, size_t output_length) {
65 uint64 random_int;
66 size_t random_int_size = sizeof(random_int);
67 for (size_t i = 0; i < output_length; i += random_int_size) {
68 random_int = base::RandUint64();
69 size_t copy_count = std::min(output_length - i, random_int_size);
70 memcpy(((uint8*)output) + i, &random_int, copy_count);
71 }
72 }
73
74 std::string RandBytesAsString(size_t length) { 64 std::string RandBytesAsString(size_t length) {
75 DCHECK_GT(length, 0u); 65 DCHECK_GT(length, 0u);
76 std::string result; 66 std::string result;
77 RandBytes(WriteInto(&result, length + 1), length); 67 RandBytes(WriteInto(&result, length + 1), length);
78 return result; 68 return result;
79 } 69 }
80 70
81 } // namespace base 71 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/rand_util_nacl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698