f81e7f6c809e9cf14cee3c52833724142a3af2d0
[j2kbench.git] / j2kbench.cc
1 #include <dcp/j2k.h>
2 #include <dcp/openjpeg_image.h>
3 #include <boost/thread.hpp>
4 #include <boost/foreach.hpp>
5 #include <list>
6 #include <iostream>
7
8 using std::list;
9 using std::cout;
10 using boost::shared_ptr;
11
12 void
13 thread (int iterations)
14 {
15         int const width = 1998;
16         int const height = 1080;
17
18         for (int i = 0; i < iterations; ++i) {
19                 shared_ptr<const dcp::OpenJPEGImage> xyz (new dcp::OpenJPEGImage (dcp::Size (width, height)));
20                 int32_t* X = xyz->data(0);
21                 int32_t* Y = xyz->data(1);
22                 int32_t* Z = xyz->data(2);
23                 for (int y = 0; y < height; ++y) {
24                         for (int x = 0; x < width; ++x)  {
25                                 *X++ = x;
26                                 *Y++ = y;
27                                 *Z++ = (x * y) % 4096;
28                         }
29                 }
30
31                 dcp::compress_j2k (xyz, 100000000, 24, false, false);
32         }
33 }
34
35 int
36 main (int argc, char* argv[])
37 {
38         if (argc < 3) {
39                 cout << "Syntax: " << argv[0] << " <threads> <iterations>\n";
40                 exit (EXIT_FAILURE);
41         }
42
43         int const num_threads = atoi (argv[1]);
44         int const num_iterations = atoi (argv[2]);
45         cout << num_threads << " threads, " << num_iterations << " iterations.\n";
46
47         struct timeval start;
48         gettimeofday (&start, 0);
49
50         list<boost::thread*> threads;
51         for (int i = 0; i < num_threads; ++i) {
52                 threads.push_back (new boost::thread (boost::bind (&thread, num_iterations)));
53         }
54
55         BOOST_FOREACH (boost::thread* i, threads) {
56                 i->join ();
57         }
58
59         struct timeval stop;
60         gettimeofday (&stop, 0);
61
62         cout << ((stop.tv_sec + double(stop.tv_usec) / 1000000) - (start.tv_sec + double(start.tv_usec) / 1000000)) << "s\n";
63 }