Use ros::Time
(or any system library for time..) :
ros::Time lasttime=ros::Time::now();
ros::Duration(3).sleep();
ros::Time currtime=ros::Time::now();
ros::Duration diff=currtime-lasttime;
cout<<"diff: "<<diff<<endl;
Note that depending on your application you may want to measure either 1) the time difference between the two calls to publish() by your publisher, or 2) the time difference between the two executions of the callback of your subscriber.
The two can be quite different depending on the amount of delay on your network and whether your subscribing node is busy doing something else when the message is published on the topic...
A robust approach is to include timestamps in the messages that you publish, so that your subscribing node can check if the messages are outdated, or even if they are not arriving in chronological order.