-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathbench_segmented_algos.cpp
More file actions
2582 lines (2303 loc) · 100 KB
/
bench_segmented_algos.cpp
File metadata and controls
2582 lines (2303 loc) · 100 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2025-2026. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
// Force aggressive function/loop alignment to eliminate instruction cache-line
// alignment noise from benchmark measurements. Without this, identical code can
// show up to 1.8x performance variation depending on where the linker happens
// to place each template instantiation relative to 64-byte cache-line boundaries.
// Requires GCC >= 9: GCC 8's #pragma GCC optimize applies optimization attributes
// to all functions in the TU, conflicting with __attribute__((always_inline)) on
// friend operators defined in headers (e.g. wrapped_iterator), causing
// -Werror=attributes errors.
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ >= 9)
#pragma GCC optimize("align-functions=64", "align-loops=32")
#elif defined(__clang__)
// Clang has no file-wide pragma for alignment. Use command-line flags:
// -falign-functions=64 -falign-loops=32
#elif defined(_MSC_VER)
// MSVC has no pragma or attribute for function/loop alignment control.
#endif
#include <algorithm>
#include <boost/container/vector.hpp>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <utility>
#include <typeinfo>
#include <boost/container/deque.hpp>
#include <boost/container/experimental/nest.hpp>
#include <boost/container/experimental/segmented_all_of.hpp>
#include <boost/container/experimental/segmented_any_of.hpp>
#include <boost/container/experimental/segmented_copy.hpp>
#include <boost/container/experimental/segmented_copy_if.hpp>
#include <boost/container/experimental/segmented_copy_n.hpp>
#include <boost/container/experimental/segmented_count.hpp>
#include <boost/container/experimental/segmented_count_if.hpp>
#include <boost/container/experimental/segmented_equal.hpp>
#include <boost/container/experimental/segmented_fill.hpp>
#include <boost/container/experimental/segmented_fill_n.hpp>
#include <boost/container/experimental/segmented_find.hpp>
#include <boost/container/experimental/segmented_find_if.hpp>
#include <boost/container/experimental/segmented_find_if_not.hpp>
#include <boost/container/experimental/segmented_find_last.hpp>
#include <boost/container/experimental/segmented_find_last_if.hpp>
#include <boost/container/experimental/segmented_find_last_if_not.hpp>
#include <boost/container/experimental/segmented_for_each.hpp>
#include <boost/container/experimental/segmented_generate.hpp>
#include <boost/container/experimental/segmented_generate_n.hpp>
#include <boost/container/experimental/segmented_is_partitioned.hpp>
#include <boost/container/experimental/segmented_is_sorted.hpp>
#include <boost/container/experimental/segmented_merge.hpp>
#include <boost/container/experimental/segmented_mismatch.hpp>
#include <boost/container/experimental/segmented_none_of.hpp>
#include <boost/container/experimental/segmented_is_sorted_until.hpp>
#include <boost/container/experimental/segmented_partition.hpp>
#include <boost/container/experimental/segmented_partition_copy.hpp>
#include <boost/container/experimental/segmented_partition_point.hpp>
#include <boost/container/experimental/segmented_remove.hpp>
#include <boost/container/experimental/segmented_remove_if.hpp>
#include <boost/container/experimental/segmented_remove_copy.hpp>
#include <boost/container/experimental/segmented_remove_copy_if.hpp>
#include <boost/container/experimental/segmented_replace.hpp>
#include <boost/container/experimental/segmented_replace_if.hpp>
#include <boost/container/experimental/segmented_reverse.hpp>
#include <boost/container/experimental/segmented_reverse_copy.hpp>
#include <boost/container/experimental/segmented_search.hpp>
#include <boost/container/experimental/segmented_search_n.hpp>
#include <boost/container/experimental/segmented_set_difference.hpp>
#include <boost/container/experimental/segmented_set_intersection.hpp>
#include <boost/container/experimental/segmented_set_symmetric_difference.hpp>
#include <boost/container/experimental/segmented_set_union.hpp>
#include <boost/container/experimental/segmented_stable_partition.hpp>
#include <boost/container/experimental/segmented_swap_ranges.hpp>
#include <boost/container/experimental/segmented_transform.hpp>
#include <boost/container/experimental/wrapped_iterator.hpp>
#include "../bench/bench_utils.hpp"
#define BOOST_CONTAINER_BENCH_SEGMENTED_GROUP 10
#define BOOST_CONTAINER_BENCH_SEGMENTED_BIDIR_ENABLED 0
namespace bc = boost::container;
//////////////////////////////////////////////////////////////////////////////
// Value types
//////////////////////////////////////////////////////////////////////////////
class MyInt
{
int int_;
public:
inline explicit MyInt(int i = 0)
: int_(i)
{}
inline MyInt(const MyInt &other)
: int_(other.int_)
{}
inline MyInt & operator=(const MyInt &other)
{
int_ = other.int_;
return *this;
}
inline ~MyInt()
{
int_ = 0;
}
inline int int_value() const { return int_; }
friend inline bool operator==(const MyInt& a, const MyInt& b) { return a.int_ == b.int_; }
friend inline bool operator!=(const MyInt& a, const MyInt& b) { return a.int_ != b.int_; }
friend inline bool operator<(const MyInt& a, const MyInt& b) { return a.int_ < b.int_; }
friend inline bool operator>(const MyInt& a, const MyInt& b) { return a.int_ > b.int_; }
};
class MyFatInt
{
int int0_;
int int1_;
int int2_;
int int3_;
int int4_;
int int5_;
int int6_;
int int7_;
public:
inline explicit MyFatInt(int i = 0)
: int0_(i++)
, int1_(i++)
, int2_(i++)
, int3_(i++)
, int4_(i++)
, int5_(i++)
, int6_(i++)
, int7_(i++)
{}
inline MyFatInt(const MyFatInt &other)
: int0_(other.int0_)
, int1_(other.int1_)
, int2_(other.int2_)
, int3_(other.int3_)
, int4_(other.int4_)
, int5_(other.int5_)
, int6_(other.int6_)
, int7_(other.int7_)
{}
inline MyFatInt & operator=(const MyFatInt &other)
{
int0_ = other.int0_;
int1_ = other.int1_;
int2_ = other.int2_;
int3_ = other.int3_;
int4_ = other.int4_;
int5_ = other.int5_;
int6_ = other.int6_;
int7_ = other.int7_;
return *this;
}
inline ~MyFatInt()
{
int0_ = 0;
int1_ = 0;
int2_ = 0;
int3_ = 0;
int4_ = 0;
int5_ = 0;
int6_ = 0;
int7_ = 0;
}
inline int int_value() const { return int0_; }
friend inline bool operator==(const MyFatInt& a, const MyFatInt& b) { return a.int0_ == b.int0_; }
friend inline bool operator!=(const MyFatInt& a, const MyFatInt& b) { return a.int0_ != b.int0_; }
friend inline bool operator<(const MyFatInt& a, const MyFatInt& b) { return a.int0_ < b.int0_; }
friend inline bool operator>(const MyFatInt& a, const MyFatInt& b) { return a.int0_ > b.int0_; }
friend inline bool operator<=(const MyFatInt& a, const MyFatInt& b) { return a.int0_ <= b.int0_; }
friend inline bool operator>=(const MyFatInt& a, const MyFatInt& b) { return a.int0_ >= b.int0_; }
};
inline int int_value(int x) { return x; }
inline int int_value(const MyInt& x) { return x.int_value(); }
inline int int_value(const MyFatInt& x) { return x.int_value(); }
//////////////////////////////////////////////////////////////////////////////
// Functors
//////////////////////////////////////////////////////////////////////////////
template<class T>
struct add_one
{
T operator()(const T& x) const { return T(int_value(x) + 1); }
};
template<class T>
struct is_odd
{
bool operator()(const T& x) const { return (int_value(x) & 1) != 0; }
};
template<class T>
struct summer
{
int sum;
summer() : sum(0) {}
void operator()(const T& x) { sum = static_cast<int>((static_cast<unsigned>(sum) + static_cast<unsigned>(int_value(x)))); }
};
template<class T>
struct is_negative
{
bool operator()(const T& x) const { return int_value(x) < 0; }
};
template<class T>
struct is_zero_or_positive
{
bool operator()(const T& x) const { return int_value(x) >= 0; }
};
template<class T>
struct counter
{
int n;
counter() : n(0) {}
T operator()() { return T(n++); }
};
template<class T>
class equal_to_ref
{
typedef T value_type;
const value_type &t_;
public:
BOOST_CONTAINER_FORCEINLINE explicit equal_to_ref(const value_type &t)
: t_(t)
{}
template <class U>
BOOST_CONTAINER_FORCEINLINE bool operator()(const U &t)const
{
return t_ == t;
}
};
template<class T>
class less_than_ref
{
typedef T value_type;
const value_type& t_;
public:
BOOST_CONTAINER_FORCEINLINE explicit less_than_ref(const value_type& t)
: t_(t)
{
}
template <class U>
BOOST_CONTAINER_FORCEINLINE bool operator()(const U& t)const
{
return t < t_;
}
};
template<class T>
class less_and_greater_ref
{
typedef T value_type;
const value_type& l_;
const value_type& g_;
public:
BOOST_CONTAINER_FORCEINLINE explicit less_and_greater_ref(const value_type& l, const value_type& g)
: l_(l), g_(g)
{
}
template <class U>
BOOST_CONTAINER_FORCEINLINE bool operator()(const U& t)const
{
return t < l_ || t > g_ ;
}
};
template<class T>
class unequal_to_ref
{
typedef T value_type;
const value_type &t_;
public:
BOOST_CONTAINER_FORCEINLINE explicit unequal_to_ref(const value_type &t)
: t_(t)
{}
template <class U>
BOOST_CONTAINER_FORCEINLINE bool operator()(const U &t)const
{
return t_ != t;
}
};
//////////////////////////////////////////////////////////////////////////////
// C++03 fallbacks for C++11-only <algorithm> functions
//////////////////////////////////////////////////////////////////////////////
namespace bench_detail {
#if BOOST_CXX_VERSION < 201103L
template<class InIt, class Pred>
bool all_of(InIt first, InIt last, Pred pred)
{
for (; first != last; ++first)
if (!pred(*first)) return false;
return true;
}
template<class InIt, class Pred>
bool any_of(InIt first, InIt last, Pred pred)
{
for (; first != last; ++first)
if (pred(*first)) return true;
return false;
}
template<class InIt, class Pred>
bool none_of(InIt first, InIt last, Pred pred)
{
for (; first != last; ++first)
if (pred(*first)) return false;
return true;
}
template <class InpIter, class Sent, class Pred>
inline InpIter find_if_not(InpIter first, Sent last, Pred pred)
{
return std::find_if(first, last, boost::container::not_pred<Pred>(pred));
}
template<class InIt, class OutIt, class Pred>
OutIt copy_if(InIt first, InIt last, OutIt d_first, Pred pred)
{
for (; first != last; ++first)
if (pred(*first))
*d_first++ = *first;
return d_first;
}
template<class InIt, class Size, class OutIt>
OutIt copy_n(InIt first, Size count, OutIt result)
{
for (Size i = 0; i < count; ++i, ++first, ++result)
*result = *first;
return result;
}
template<class FwdIt>
bool is_sorted(FwdIt first, FwdIt last)
{
if (first != last) {
FwdIt next = first;
for (; ++next != last; first = next)
if (*next < *first)
return false;
}
return true;
}
template<class FwdIt>
FwdIt is_sorted_until(FwdIt first, FwdIt last)
{
if (first != last) {
FwdIt next = first;
for (; ++next != last; first = next)
if (*next < *first)
return next;
}
return last;
}
template<class InIt, class Pred>
bool is_partitioned(InIt first, InIt last, Pred pred)
{
for (; first != last; ++first)
if (!pred(*first)) break;
for (; first != last; ++first)
if (pred(*first)) return false;
return true;
}
template<class InIt, class OutIt1, class OutIt2, class Pred>
std::pair<OutIt1, OutIt2>
partition_copy(InIt first, InIt last,
OutIt1 out_true, OutIt2 out_false, Pred pred)
{
for (; first != last; ++first) {
if (pred(*first))
*out_true++ = *first;
else
*out_false++ = *first;
}
return std::pair<OutIt1, OutIt2>(out_true, out_false);
}
template<class FwdIt, class Pred>
FwdIt partition_point(FwdIt first, FwdIt last, Pred pred)
{
for (; first != last; ++first)
if (!pred(*first)) return first;
return last;
}
#else
using std::all_of;
using std::any_of;
using std::none_of;
using std::find_if_not;
using std::copy_if;
using std::copy_n;
using std::is_sorted;
using std::is_sorted_until;
using std::is_partitioned;
using std::partition_copy;
using std::partition_point;
#endif
#if BOOST_CXX_VERSION < 201402L
// Portable two-range std::mismatch (std::mismatch with 4 args is C++14).
template<class InIt1, class InIt2>
std::pair<InIt1, InIt2> mismatch(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2)
{
while (first1 != last1 && first2 != last2 && *first1 == *first2) {
++first1; ++first2;
}
return std::pair<InIt1, InIt2>(first1, first2);
}
#else
using std::mismatch;
#endif
template<class BidirIt, class T>
BidirIt find_last_dispatch(BidirIt first, BidirIt last, const T& val, std::bidirectional_iterator_tag)
{
BidirIt it = last;
while (it != first) {
--it;
if (*it == val) return it;
}
return last;
}
template<class FwdIt, class T>
FwdIt find_last_dispatch(FwdIt first, FwdIt last, const T& val, std::forward_iterator_tag)
{
FwdIt result = last;
for (; first != last; ++first)
if (*first == val) result = first;
return result;
}
template<class It, class T>
BOOST_CONTAINER_FORCEINLINE It find_last(It first, It last, const T& val)
{
typedef typename std::iterator_traits<It>::iterator_category cat;
return find_last_dispatch(first, last, val, cat());
}
template<class BidirIt, class Pred>
BidirIt find_last_if_dispatch(BidirIt first, BidirIt last, Pred pred, std::bidirectional_iterator_tag)
{
BidirIt it = last;
while (it != first) {
--it;
if (pred(*it)) return it;
}
return last;
}
template<class FwdIt, class Pred>
FwdIt find_last_if_dispatch(FwdIt first, FwdIt last, Pred pred, std::forward_iterator_tag)
{
FwdIt result = last;
for (; first != last; ++first)
if (pred(*first)) result = first;
return result;
}
template<class It, class Pred>
BOOST_CONTAINER_FORCEINLINE It find_last_if(It first, It last, Pred pred)
{
typedef typename std::iterator_traits<It>::iterator_category cat;
return find_last_if_dispatch(first, last, pred, cat());
}
template<class BidirIt, class Pred>
BidirIt find_last_if_not_dispatch(BidirIt first, BidirIt last, Pred pred, std::bidirectional_iterator_tag)
{
BidirIt it = last;
while (it != first) {
--it;
if (!pred(*it)) return it;
}
return last;
}
template<class FwdIt, class Pred>
FwdIt find_last_if_not_dispatch(FwdIt first, FwdIt last, Pred pred, std::forward_iterator_tag)
{
FwdIt result = last;
for (; first != last; ++first)
if (!pred(*first)) result = first;
return result;
}
template<class It, class Pred>
BOOST_CONTAINER_FORCEINLINE It find_last_if_not(It first, It last, Pred pred)
{
typedef typename std::iterator_traits<It>::iterator_category cat;
return find_last_if_not_dispatch(first, last, pred, cat());
}
//Not benchmarked:
//inplace_merge
//not implemented (c++03)
//find_end
//find_first_of
//adjacent_find
//copy_backward
//move
//move_backward
//transform
//replace_copy
//replace_copy_if
//unique
//unique_copy
//rotate
//rotate_copy
//max_element
//min_element
//minmax_element
//nth_element
//includes
//set_union
//lexicographical_compare
// -- sorting --
//sort (random-access non-implementable?)
//stable_sort (random-access non-implementable?)
//partial_sort (random-access non-implementable?)
//partial_sort_copy(random-access non-implementable?)
// -- binary search --
//lower_bound (binary/random-access non-implementable?)
//upper_bound (binary/random-access non-implementable?)
//equal_range (binary/random-access non-implementable?)
//binary_search (binary/random-access non-implementable?)
// -- heap --
//push_heap (binary/random-access non-implementable?)
//pop_heap (binary/random-access non-implementable?)
//make_heap (binary/random-access non-implementable?)
//sort_heap (binary/random-access non-implementable?)
// -- permutation --
//next_permutation
//prev_permutation
//is_permutation
// -- numeric --
//iota
//accumulate
//inner_product
//adjacent_difference
//partial_sum
//not implemented (c++11):
//move
//move_backward
//shuffle (random-access non-implementable?)
//is_heap (random-access non-implementable?)
//is_heap_until (random-access non-implementable?)
//not implemented (c++14)
//equal with two full ranges
//not implemented (c++17)
//for_each_n
//random_shuffle (random-access non-implementable?)
//sample (random-access non-implementable?)
//reduce
//exclusive_scan
//inclusive_scan
//transform_reduce
//transform_exclusive_scan
//transform_inclusive_scan
//not implemented (c++20)
//shift_left
//shift_right
//lexicographical_compare_three_way
//range-based?
//contains
//contains_subrange
//starts_with
//ends_with
//fold_left
//fold_left_first
//fold_right
//fold_right_last
//fold_left_with_iter
//fold_left_first_with_iter
//generate_random
} // namespace bench_detail
//////////////////////////////////////////////////////////////////////////////
// Fill helpers
//////////////////////////////////////////////////////////////////////////////
template<class C>
void fill_test_data(C& c, std::size_t n)
{
typedef typename C::value_type VT;
for (std::size_t i = 0; i < n; ++i)
c.push_back(VT(static_cast<int>(i)));
}
template<class T, class A, class O>
void fill_test_data(bc::nest<T,A,O>& c, std::size_t n)
{
for (std::size_t i = 0; i < n; ++i)
c.insert(T(static_cast<int>(i)));
}
//////////////////////////////////////////////////////////////////////////////
// Benchmark helpers
//////////////////////////////////////////////////////////////////////////////
inline double calc_ns_per_elem(boost::move_detail::nanosecond_type ns,
std::size_t iters, std::size_t elems)
{
return double(ns) / double(iters * elems);
}
inline void print_subheader()
{
std::cout << std::left << std::setw(24) << "< algo >"
<< std::right << std::setw(16) << "< nsg/seg >"
<< std::right << std::setw(16) << "< std/seg >"
<< std::right << std::setw(16) << "< std/nsg >"
<< '\n';
}
struct geomean_accumulator
{
double std_over_seg_log_sum, std_over_nsg_log_sum, ns_over_std_over_seg_log_sum;
int std_over_seg_count, std_over_nsg_count, nseg_over_seg_count;
void reset()
{
std_over_seg_log_sum = std_over_nsg_log_sum = ns_over_std_over_seg_log_sum = 0.0;
std_over_seg_count = std_over_nsg_count = nseg_over_seg_count = 0;
}
void add_std_over_seg(double r)
{
if(r > 0.0) {
std_over_seg_log_sum += std::log(r);
++std_over_seg_count;
}
}
void add_std_over_nsg(double r)
{
if(r > 0.0) {
std_over_nsg_log_sum += std::log(r);
++std_over_nsg_count;
}
}
void add_nsg_over_seg(double r)
{
if(r > 0.0) {
ns_over_std_over_seg_log_sum += std::log(r);
++nseg_over_seg_count;
}
}
double std_over_seg_result() const
{
return std_over_seg_count > 0 ? std::exp(std_over_seg_log_sum / std_over_seg_count) : 0.0;
}
double std_over_nsg_result() const
{
return std_over_nsg_count > 0 ? std::exp(std_over_nsg_log_sum / std_over_nsg_count) : 0.0;
}
double nsg_over_seg_result() const
{
return nseg_over_seg_count > 0 ? std::exp(ns_over_std_over_seg_log_sum / nseg_over_seg_count) : 0.0;
}
} g_geomean = { 0.0, 0.0, 0.0, 0, 0, 0 };
inline void print_ratio(const char* algo, const char*,
double std_ns, double seg_ns, double nsg_ns)
{
double nsg_seg_ratio = (seg_ns > 0.0) ? nsg_ns / seg_ns : 0.0;
double std_seg_ratio = (seg_ns > 0.0) ? std_ns / seg_ns : 0.0;
double std_nsg_ratio = (std_ns > 0.0) ? std_ns / nsg_ns : 0.0;
g_geomean.add_std_over_seg(std_seg_ratio);
g_geomean.add_std_over_nsg(std_nsg_ratio);
g_geomean.add_nsg_over_seg(nsg_seg_ratio);
std::cout << std::left << std::setw(24) << algo
<< std::right << std::setw(16) << std::fixed << std::setprecision(2) << nsg_seg_ratio
<< std::right << std::setw(16) << std::fixed << std::setprecision(2) << std_seg_ratio
<< std::right << std::setw(16) << std::fixed << std::setprecision(2) << std_nsg_ratio
<< '\n';
}
//////////////////////////////////////////////////////////////////////////////
// Measurement infrastructure
//////////////////////////////////////////////////////////////////////////////
template <class F, class ResetF>
inline boost::move_detail::nanosecond_type measure_batch(std::size_t iters, F f, ResetF reset_f)
{
cpu_timer t;
std::size_t n_ = (iters + 7) / 8;
t.resume();
switch (iters % 8) {
case 0: do {
t.resume();
f(); BOOST_FALLTHROUGH;
case 7: f(); BOOST_FALLTHROUGH;
case 6: f(); BOOST_FALLTHROUGH;
case 5: f(); BOOST_FALLTHROUGH;
case 4: f(); BOOST_FALLTHROUGH;
case 3: f(); BOOST_FALLTHROUGH;
case 2: f(); BOOST_FALLTHROUGH;
case 1: f();
t.stop();
if (BOOST_UNLIKELY(--n_ == 0)) break;
reset_f();
} while (true);
}
return t.elapsed();
}
struct noop_reset {
BOOST_CONTAINER_FORCEINLINE void operator()() {}
};
template <class F1, class R1, class F2, class R2, class F3, class R3>
inline void compare_batch(std::size_t iters, std::size_t nelems,
F1 std_op, R1 std_reset, F2 seg_op, R2 seg_reset,
F3 nsg_op, R3 nsg_reset,
const char* label, const char* cname)
{
typedef boost::move_detail::nanosecond_type ns_type;
ns_type t1 = measure_batch(iters, std_op, std_reset);
ns_type t2 = measure_batch(iters, seg_op, seg_reset);
ns_type t3 = measure_batch(iters, nsg_op, nsg_reset);
print_ratio(label, cname, calc_ns_per_elem(t1, iters, nelems),
calc_ns_per_elem(t2, iters, nelems), calc_ns_per_elem(t3, iters, nelems));
}
template <class F1, class F2, class F3>
inline void compare_batch(std::size_t iters, std::size_t nelems,
F1 std_op, F2 seg_op, F3 nsg_op,
const char* label, const char* cname)
{
compare_batch(iters, nelems, std_op, noop_reset(), seg_op, noop_reset(),
nsg_op, noop_reset(), label, cname);
}
//////////////////////////////////////////////////////////////////////////////
// Benchmark operation functors
//////////////////////////////////////////////////////////////////////////////
namespace bench_ops {
template<bool Wrap> struct iter_w {
template<class It> static BOOST_CONTAINER_FORCEINLINE
It wrap(It i) { return i; }
};
template<> struct iter_w<true> {
template<class It> static BOOST_CONTAINER_FORCEINLINE
bc::wrapped_iterator<It> wrap(It i) { return bc::wrapped_iterator<It>(i); }
};
template<bool Wrap, class It> struct iter_wt { typedef It type; };
template<class It> struct iter_wt<true, It> { typedef bc::wrapped_iterator<It> type; };
template<class C>
struct batch_state {
C c1, c2, c3, c4, c5, c6, c7, c8;
C* cs[8];
int idx;
const C &orig;
batch_state(const C &c_)
: c1(c_), c2(c_), c3(c_), c4(c_), c5(c_), c6(c_), c7(c_), c8(c_), idx(0), orig(c_)
{ cs[0]=&c1; cs[1]=&c2; cs[2]=&c3; cs[3]=&c4; cs[4]=&c5; cs[5]=&c6; cs[6]=&c7; cs[7]=&c8; }
};
template<class C>
struct batch_reset {
batch_state<C> &bs;
batch_reset(batch_state<C> &b) : bs(b) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ bs.idx = 0; bs.c1 = bs.c2 = bs.c3 = bs.c4 = bs.c5 = bs.c6 = bs.c7 = bs.c8 = bs.orig; }
};
// --- all_of ---
template<class C, class Pred>
struct std_all_of {
const C &c; Pred pred; int &result;
std_all_of(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = bench_detail::all_of(c.begin(), c.end(), pred) ? 1 : 0; escape(&result); }
};
template<class C, class Pred, bool Wrap = false>
struct seg_all_of {
const C &c; Pred pred; int &result;
seg_all_of(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = bc::segmented_all_of(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), pred) ? 1 : 0; escape(&result); }
};
// --- any_of ---
template<class C, class Pred>
struct std_any_of {
const C &c; Pred pred; int &result;
std_any_of(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = bench_detail::any_of(c.begin(), c.end(), pred) ? 1 : 0; escape(&result); }
};
template<class C, class Pred, bool Wrap = false>
struct seg_any_of {
const C &c; Pred pred; int &result;
seg_any_of(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = bc::segmented_any_of(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), pred) ? 1 : 0; escape(&result); }
};
// --- none_of ---
template<class C, class Pred>
struct std_none_of {
const C &c; Pred pred; int &result;
std_none_of(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = bench_detail::none_of(c.begin(), c.end(), pred) ? 1 : 0; escape(&result); }
};
template<class C, class Pred, bool Wrap = false>
struct seg_none_of {
const C &c; Pred pred; int &result;
seg_none_of(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = bc::segmented_none_of(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), pred) ? 1 : 0; escape(&result); }
};
// --- for_each ---
template<class C>
struct std_for_each {
typedef typename C::value_type VT;
const C &c; int &result;
std_for_each(const C &c_, int &r_) : c(c_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ summer<VT> s; clobber(); s = std::for_each(c.begin(), c.end(), s); result = s.sum; escape(&result); }
};
template<class C, bool Wrap = false>
struct seg_for_each {
typedef typename C::value_type VT;
const C &c; int &result;
seg_for_each(const C &c_, int &r_) : c(c_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ summer<VT> s; clobber(); s = bc::segmented_for_each(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), s); result = s.sum; escape(&result); }
};
// --- copy ---
template<class C, class OutT>
struct std_copy {
const C &c; OutT &out;
std_copy(const C &c_, OutT &o_) : c(c_), out(o_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); std::copy(c.begin(), c.end(), out.begin()); escape(&*out.begin()); }
};
template<class C, class OutT, bool Wrap = false>
struct seg_copy {
const C &c; OutT &out;
seg_copy(const C &c_, OutT &o_) : c(c_), out(o_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); bc::segmented_copy(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), iter_w<Wrap>::wrap(out.begin())); escape(&*out.begin()); }
};
// --- copy_if ---
template<class C, class OutT, class Pred>
struct std_copy_if {
const C &c; OutT &out; Pred pred;
std_copy_if(const C &c_, OutT &o_, Pred p_) : c(c_), out(o_), pred(p_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); bench_detail::copy_if(c.begin(), c.end(), out.begin(), pred); escape(&*out.begin()); }
};
template<class C, class OutT, class Pred, bool Wrap = false>
struct seg_copy_if {
const C &c; OutT &out; Pred pred;
seg_copy_if(const C &c_, OutT &o_, Pred p_) : c(c_), out(o_), pred(p_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); bc::segmented_copy_if(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), iter_w<Wrap>::wrap(out.begin()), pred); escape(&*out.begin()); }
};
// --- fill ---
template<class C>
struct std_fill {
C &c2; const typename C::value_type &val;
std_fill(C &c_, const typename C::value_type &v_) : c2(c_), val(v_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); std::fill(c2.begin(), c2.end(), val); escape(&c2); }
};
template<class C, bool Wrap = false>
struct seg_fill {
C &c2; const typename C::value_type &val;
seg_fill(C &c_, const typename C::value_type &v_) : c2(c_), val(v_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); bc::segmented_fill(iter_w<Wrap>::wrap(c2.begin()), iter_w<Wrap>::wrap(c2.end()), val); escape(&c2); }
};
// --- count ---
template<class C>
struct std_count {
const C &c; const typename C::value_type &val; int &result;
std_count(const C &c_, const typename C::value_type &v_, int &r_) : c(c_), val(v_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = static_cast<int>(std::count(c.begin(), c.end(), val)); escape(&result); }
};
template<class C, bool Wrap = false>
struct seg_count {
const C &c; const typename C::value_type &val; int &result;
seg_count(const C &c_, const typename C::value_type &v_, int &r_) : c(c_), val(v_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = static_cast<int>(bc::segmented_count(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), val)); escape(&result); }
};
// --- count_if ---
template<class C, class Pred>
struct std_count_if {
const C &c; Pred pred; int &result;
std_count_if(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = static_cast<int>(std::count_if(c.begin(), c.end(), pred)); escape(&result); }
};
template<class C, class Pred, bool Wrap = false>
struct seg_count_if {
const C &c; Pred pred; int &result;
seg_count_if(const C &c_, Pred p_, int &r_) : c(c_), pred(p_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); result = static_cast<int>(bc::segmented_count_if(iter_w<Wrap>::wrap(c.begin()), iter_w<Wrap>::wrap(c.end()), pred)); escape(&result); }
};
// --- find ---
template<class C>
struct std_find {
typedef typename C::const_iterator cit_t;
const C &c; const typename C::value_type &val; int &result;
std_find(const C &c_, const typename C::value_type &v_, int &r_) : c(c_), val(v_), result(r_) {}
BOOST_CONTAINER_FORCEINLINE void operator()()
{ clobber(); cit_t it = std::find(c.begin(), c.end(), val); result = (it == c.end()) ? 0 : 1; escape(&result); }
};
template<class C, bool Wrap = false>
struct seg_find {
typedef typename iter_wt<Wrap, typename C::const_iterator>::type cit_t;
const C &c; const typename C::value_type &val; int &result;
seg_find(const C &c_, const typename C::value_type &v_, int &r_) : c(c_), val(v_), result(r_) {}