On 10/21/12 21:44, Liviu Nicoara wrote:
On 10/21/12 21:11, Liviu Nicoara wrote:
Hi all,
I ran the attached test case, s.cpp, and timed it:
Gosh darn it, attached the wrong files. Here are the right ones.
Made the same mistake twice. My apologies for the clutter. Here are the
right ones.
Liviu
* Deep-copy
$ for f in 16 8 4 2 1; do time ./s $f 50000000; done
16, 50000000 0m9.438s 2m15.615s 0m10.799s
8, 50000000 0m6.973s 0m55.579s 0m0.006s
4, 50000000 0m6.942s 0m27.505s 0m0.001s
2, 50000000 0m6.888s 0m13.753s 0m0.002s
1, 50000000 0m6.800s 0m6.799s 0m0.001s
* Ref-counted
$ for f in 16 8 4 2 1; do time ./s $f 50000000; done
16, 50000000 2m19.098s 2m36.278s 30m38.488s
8, 50000000 1m47.757s 2m7.278s 10m18.418s
4, 50000000 4m27.425s 5m56.620s 10m13.892s
2, 50000000 0m17.667s 0m22.560s 0m10.695s
1, 50000000 0m2.923s 0m2.921s 0m0.001s
#include <iostream>
#include <locale>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <pthread.h>
#include <unistd.h>
#define MAX_THREADS 128
static long nloops = 10000000, nthreads = 16;
static bool volatile pwait = true;
////////////////////////////////////////////////////////////////////////
extern "C" {
static void*
f (void* pv)
{
std::string const& s = *reinterpret_cast< std::string* > (pv);
volatile unsigned long n = 0;
while (pwait) ;
for (int i = 0; i < nloops; ++i) {
#if !defined (NO_DEEP_COPY)
const std::string tmp (s.c_str (), s.size ());
#else
const std::string tmp = s;
#endif // NO_DEEP_COPY
n += strlen (tmp.c_str ());
}
return (void*)n;
}
} // extern "C"
int
main (int argc, char** argv)
{
switch (argc) {
case 3:
nloops = atol (argv [2]);
case 2:
nthreads = atol (argv [1]);
break;
}
pthread_t tid [MAX_THREADS] = { 0 };
if (nthreads > MAX_THREADS)
nthreads = MAX_THREADS;
printf ("%ld, %ld\n", nthreads, nloops);
pthread_setconcurrency (nthreads);
std::string s ("Hello, world.");
for (int i = 0; i < nthreads; ++i) {
if (pthread_create (tid + i, 0, f, &s))
exit (-1);
}
usleep (50);
pwait = false;
for (int i = 0; i < nthreads; ++i) {
if (tid [i])
pthread_join (tid [i], 0);
}
return 0;
}