title: ZeroMQ - pub / sub latency
body: <p>I'm looking into ZeroMQ to see if it's a fit for a soft-realtime application. I was very pleased to see that the latency for small payloads were in the range of 30 micro-seconds or so. However in my simple tests, I'm getting about 300 micro-seconds.</p>
  
  <p>I have a simple publisher and subscriber, basically copied from examples off the web and I'm sending one byte through localhost.</p>
  
  <p>I've played around for about two days w/ different <code>sockopts</code> and I'm striking out. </p>
  
  <p>Any help would be appreciated!</p>
  
  <p><strong>pub</strong>lisher:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;zmq.hpp&gt;
  #include &lt;unistd.h&gt;
  
  #include &lt;sys/time.h&gt;
  
  
  int main()
  {
      zmq::context_t context (1);
      zmq::socket_t publisher (context, ZMQ_PUB);
      publisher.bind("tcp://*:5556");
  
      struct timeval timeofday;
      zmq::message_t msg(1);
      while(true)
      {
          gettimeofday(&amp;timeofday,NULL);
          publisher.send(msg);
          std::cout &lt;&lt; timeofday.tv_sec &lt;&lt; ", " &lt;&lt; timeofday.tv_usec &lt;&lt; std::endl;
          usleep(1000000);
      } 
  }  
  </code></pre>
  
  <p><strong>sub</strong>scriber:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;zmq.hpp&gt;
  #include &lt;sys/time.h&gt;
  
  
  int main()
  {
      zmq::context_t context (1);
      zmq::socket_t subscriber (context, ZMQ_SUB);
      subscriber.connect("tcp://localhost:5556");
      subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
  
      struct timeval timeofday;
      zmq::message_t update;
      while(true)
      {
          subscriber.recv(&amp;update);
          gettimeofday(&amp;timeofday,NULL);
          std::cout &lt;&lt; timeofday.tv_sec &lt;&lt; ", " &lt;&lt; timeofday.tv_usec &lt;&lt; std::endl;
      }
  }
  </code></pre>