Skip to content

Instantly share code, notes, and snippets.

@peadar
Last active August 29, 2015 14:04
Show Gist options
  • Save peadar/125cb9d64bb469fb2e99 to your computer and use it in GitHub Desktop.
Save peadar/125cb9d64bb469fb2e99 to your computer and use it in GitHub Desktop.
Timing for passing C++ strings by reference or value
#include <string>
int
fRef(const std::string &v)
{
}
int
fValue(std::string v)
{
}
#include <string>
#include <stdlib.h>
#include <unistd.h>
extern int fValue(std::string);
extern int fRef(const std::string &f);
int
main(int argc, char *argv[])
{
int c;
enum Type {
ByRef,
ByValue,
None
};
std::string data = "hello world";
int count = 10000;
Type t = None;
while ((c = getopt(argc, argv, "c:d:rv")) != -1) {
switch (c) {
case 'c': count = strtol(optarg, 0, 0); break;
case 'd': data = optarg; break;
case 'r': t = ByRef; break;
case 'v': t = ByValue; break;
}
}
switch (t) {
case ByRef:
while (count--)
fRef(data);
break;
case ByValue:
while (count--)
fValue(data);
break;
case None:
abort();
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment