-
-
Save peadar/125cb9d64bb469fb2e99 to your computer and use it in GitHub Desktop.
Timing for passing C++ strings by reference or value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
int | |
fRef(const std::string &v) | |
{ | |
} | |
int | |
fValue(std::string v) | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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