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

Message slow to construct for small message #143

Open
ggggggggg opened this issue Apr 13, 2017 · 1 comment
Open

Message slow to construct for small message #143

ggggggggg opened this issue Apr 13, 2017 · 1 comment

Comments

@ggggggggg
Copy link
Contributor

See this thread for context. Consider the following code (requires PR #141). message takes about 140 ns, while Message takes 3400 ns. Something like message should be available or the performance of Message should be made comparable for small payloads.

using ZMQ, BenchmarkTools

function message(s)
  b = IOBuffer()
  write(b,s)
  m = Message(b.size)
  m[:]=b.data
  m
end

@benchmark message("1")
@benchmark Message("1")
@ggggggggg
Copy link
Contributor Author

I wrote a version of message with only one copy, which is a bit faster than the previous one. Then I benchmarked all 3 version (my two, plus ZMQ.Message) vs length of string. ZMQ.Message appears to win for strings longer than roughly 250 bytes. But I think there may be something else wrong, when I run the benchmark, it takes much longer to complete the portion for Message than for the other two versions, despite the fact that the sum of minimum times across all benchmarks is lower. Nonetheless, here is the plot I generated, followed by the code I used.

afasfaf

using ZMQ, BenchmarkTools, PyPlot
function Base.setindex!(m::Message, v, i::Integer)
    @boundscheck if i < 1 || i > length(m)
        throw(BoundsError())
    end
    unsafe_store!(pointer(m), v, i)
end
function message(x)
  m = Message(sizeof(x))
  mb = Base.AbstractIOBuffer(m,true,true,true,false,length(m))
  write(mb,x)
  m
end

function message_cp(s)
  b = IOBuffer()
  write(b,s)
  m = Message(b.size)
  m[:]=b.data
  m
end

function benchmark_length(f,l)
  s = randstring(l)
  b=@benchmark $f($s) evals=10
  minimum(b.times)
end
lengths = 2.^(0:24)
message_mtimes = [benchmark_length(message,l) for l in lengths]
message_cp_mtimes = [benchmark_length(message_cp,l) for l in lengths]
Message_mtimes = [benchmark_length(Message,l) for l in lengths]

figure()
plot(lengths, message_mtimes, label="message")
plot(lengths, message_cp_mtimes, label="message_cp")
plot(lengths, Message_mtimes, label="Message")
xlabel("bytes")
ylabel("minimum time to construct (ns)")
legend(loc="best")
xscale("log")
yscale("log")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant