Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve countlines() performance #11947

Merged
merged 2 commits into from
Jun 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,15 @@ export countlines, readdlm, readcsv, writedlm, writecsv
const invalid_dlm = Char(0xfffffffe)
const offs_chunk_size = 5000

countlines(nameorfile) = countlines(nameorfile, '\n')
function countlines(filename::AbstractString, eol::Char)
open(filename) do io
countlines(io, eol)
end
end
function countlines(io::IO, eol::Char)
if !isascii(eol)
throw(ArgumentError("only ASCII line terminators are supported"))
end
countlines(f::AbstractString,eol::Char='\n') = open(io->countlines(io,eol),f)::Int
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting tidbit for others out there: while the open(func, file) can be quick and tidy, it's also quite subtle from a type stability perspective. Without the ::Int assertion here, @code_warntype shows that the return type for this method is Any, even though countlines itself is type-stable.

function countlines(io::IO, eol::Char='\n')
isascii(eol) || throw(ArgumentError("only ASCII line terminators are supported"))
a = Array(UInt8, 8192)
nl = 0
preceded_by_eol = true
while !eof(io)
nb = readbytes!(io, a)
for i=1:nb
if Char(a[i]) == eol
preceded_by_eol = true
elseif preceded_by_eol
preceded_by_eol = false
nl+=1
end
@simd for i=1:nb
@inbounds nl += a[i] == eol
end
end
nl
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/io-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ General I/O

.. function:: countlines(io,[eol::Char])

Read io until the end of the stream/file and count the number of non-empty lines. To specify a file pass the filename as the first
Read ``io`` until the end of the stream/file and count the number of lines. To specify a file pass the filename as the first
argument. EOL markers other than '\\n' are supported by passing them as the second argument.

.. function:: PipeBuffer()
Expand Down
15 changes: 15 additions & 0 deletions test/readdlm.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

# countlines
@test countlines(IOBuffer("\n")) == 1
@test countlines(IOBuffer("\n"),'\r') == 0
@test countlines(IOBuffer("\n\n\n\n\n\n\n\n\n\n")) == 10
@test countlines(IOBuffer("\n \n \n \n \n \n \n \n \n \n")) == 10
@test countlines(IOBuffer("\r\n \r\n \r\n \r\n \r\n")) == 5
file = tempname()
open(file,"w") do f
write(f,"Spiffy header\nspectacular first row\neven better 2nd row\nalmost done\n")
end
@test countlines(file) == 4
@test countlines(file,'\r') == 0
@test countlines(file,'\n') == 4
rm(file)

isequaldlm(m1, m2, t) = isequal(m1, m2) && (eltype(m1) == eltype(m2) == t)

@test isequaldlm(readdlm(IOBuffer("1\t2\n3\t4\n5\t6\n")), [1. 2; 3 4; 5 6], Float64)
Expand Down