Skip to content

Commit

Permalink
Use std::find to locate newline.
Browse files Browse the repository at this point in the history
Benchmark on lofn to count lines in
xzcat /fs/vali0/www/data.statmt.org/ngrams/deduped_en/en.00.xz |head
-n 10000000

With std::find
real    0m0.709s
user    0m0.648s
sys     0m0.060s

Baseline
real    0m0.899s
user    0m0.820s
sys     0m0.076s

C++
int main() {
  std::ios::sync_with_stdio(false);
  std::string line;
  while (getline(std::cin, line)) {}
}
real    0m0.803s
user    0m0.536s
sys     0m0.264s
  • Loading branch information
kpu committed Apr 2, 2017
1 parent e6a600c commit f293fd9
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions util/file_piece.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,16 @@ FilePiece::FilePiece(std::istream &stream, const char *name, std::size_t min_buf
StringPiece FilePiece::ReadLine(char delim, bool strip_cr) {
std::size_t skip = 0;
while (true) {
for (const char *i = position_ + skip; i < position_end_; ++i) {
if (*i == delim) {
// End of line.
// Take 1 byte off the end if it's an unwanted carriage return.
const std::size_t subtract_cr = (
(strip_cr && i > position_ && *(i - 1) == '\r') ?
1 : 0);
StringPiece ret(position_, i - position_ - subtract_cr);
position_ = i + 1;
return ret;
}
const char *i = std::find(position_ + skip, position_end_, delim);
if (UTIL_LIKELY(i != position_end_)) {
// End of line.
// Take 1 byte off the end if it's an unwanted carriage return.
const std::size_t subtract_cr = (
(strip_cr && i > position_ && *(i - 1) == '\r') ?
1 : 0);
StringPiece ret(position_, i - position_ - subtract_cr);
position_ = i + 1;
return ret;
}
if (at_end_) {
if (position_ == position_end_) {
Expand Down

0 comments on commit f293fd9

Please sign in to comment.