Permalink
Browse files

Added e1-2.cpp: a reduced version of e1.cpp with only Dst_First_1 and…

… Dst_First_3
  • Loading branch information...
pzemtsov committed May 12, 2014
1 parent 561026d commit 01988e4f8bdff46fedf0573e54bc43332402a46b
Showing with 105 additions and 0 deletions.
  1. +105 −0 e1-2.cpp
View
105 e1-2.cpp
@@ -0,0 +1,105 @@
+/** The simplified version of e1.cpp, only used for resolving performance instability issue
+ (see article "The mystery of an unstable performance").
+ Contains simplified versions of Dst_First_1 and Dst_First_2
+ */
+
+#include <cassert>
+#include <cstdlib>
+#include <cstring>
+#include <iostream>
+#include <typeinfo>
+
+#include "timer.h"
+#include "mymacros.h"
+
+typedef unsigned char byte;
+
+static const unsigned NUM_TIMESLOTS = 32;
+static const unsigned DST_SIZE = 64;
+static const unsigned SRC_SIZE = NUM_TIMESLOTS * DST_SIZE;
+static const unsigned ITERATIONS = 1000000;
+static const unsigned REPETITIONS = 2;
+
+using namespace std;
+
+class Demux
+{
+public:
+ virtual void demux (const byte * src, unsigned src_length, byte ** dst) const = 0;
+};
+
+class Dst_First_1 : public Demux
+{
+public:
+ void demux (const byte * src, unsigned src_length, byte ** dst) const
+ {
+ for (unsigned dst_num = 0; dst_num < NUM_TIMESLOTS; ++ dst_num) {
+ for (unsigned dst_pos = 0; dst_pos < src_length / NUM_TIMESLOTS; ++ dst_pos) {
+ dst [dst_num][dst_pos] = src [dst_pos * NUM_TIMESLOTS + dst_num];
+ }
+ }
+ }
+};
+
+class Dst_First_3 : public Demux
+{
+public:
+ void demux (const byte * src, unsigned src_length, byte ** dst) const
+ {
+ for (unsigned dst_num = 0; dst_num < NUM_TIMESLOTS; ++ dst_num) {
+ for (unsigned dst_pos = 0; dst_pos < DST_SIZE; ++ dst_pos) {
+ dst [dst_num][dst_pos] = src [dst_pos * NUM_TIMESLOTS + dst_num];
+ }
+ }
+ }
+};
+
+
+byte * generate ()
+{
+ byte * buf = new byte [SRC_SIZE];
+ srand (0);
+ for (unsigned i = 0; i < SRC_SIZE; i++) buf[i] = (byte) (rand () % 256);
+ return buf;
+}
+
+byte ** allocate_dst ()
+{
+ byte ** result = new byte * [NUM_TIMESLOTS];
+ for (unsigned i = 0; i < NUM_TIMESLOTS; i++) {
+ result [i] = new byte [DST_SIZE];
+ }
+ return result;
+}
+
+void delete_dst (byte ** dst)
+{
+ for (unsigned i = 0; i < NUM_TIMESLOTS; i++) {
+ delete dst [i];
+ }
+ delete dst;
+}
+
+byte * src;
+byte ** dst;
+
+void measure (const Demux & demux)
+{
+ uint64_t t0 = currentTimeMillis ();
+ for (int i = 0; i < ITERATIONS; i++) {
+ demux.demux (src, SRC_SIZE, dst);
+ }
+ uint64_t t = currentTimeMillis () - t0;
+ cout << typeid (demux).name() << ": " << t << endl;
+}
+
+int main (void)
+{
+ src = generate ();
+ dst = allocate_dst ();
+
+ measure (Dst_First_1 ());
+ measure (Dst_First_3 ());
+
+ return 0;
+}

0 comments on commit 01988e4

Please sign in to comment.