Skip to content

Instantly share code, notes, and snippets.

@JayXon
Created April 3, 2014 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayXon/9949047 to your computer and use it in GitHub Desktop.
Save JayXon/9949047 to your computer and use it in GitHub Desktop.
Speed compare between wcscpy, lstrcpyW, while loop and memcpy
#include <iostream>
#include <cstring>
#include <functional>
#include <chrono>
#include <windows.h>
#define N 1024
uint64_t TestTime(std::function<void()> f)
{
auto t = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000000; ++i)
{
f();
}
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - t).count();
}
int main(int argc, char const *argv[])
{
wchar_t a[N], b[N], c[N], d[N], e[N], f[N];
for (int i = 0; i < N-1; ++i)
{
a[i] = rand() % 65535;
}
a[N-1] = 0;
std::cout << TestTime([&] () {wcscpy(b, a);}) << std::endl;
std::cout << TestTime([&] () {lstrcpyW(c, a);}) << std::endl;
std::cout << TestTime([&] () {int i = 0; while (d[i] = a[i]) i++;}) << std::endl;
std::cout << TestTime([&] () {memcpy(e, a, (wcslen(a) + 1) * sizeof(wchar_t));}) << std::endl;
std::cout << TestTime([&] () {memcpy(f, a, N * sizeof(wchar_t));}) << std::endl; // cheat
if (wcscmp(b, a) || wcscmp(c, a) || wcscmp(d, a) || wcscmp(e, a) || wcscmp(f, a))
{
std::cout << "Something went wrong." << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment