Boost logo

Boost :

From: John Maddock (John_Maddock_at_[hidden])
Date: 2000-01-16 07:39:21


All -

There has been some (justified?) scepticism that type_taits had anything or
much to offer in the way of optimisations, so I've put together a set of 4
examples in the file algo_opt_examples.cpp in the document vault
(http://www.egroups.com/docvault/boost/TypeTraits/version3?null=1).

In order to be familiar the examples are mostly standard library
algorithms:

 opt::copy - optimised for trivial copy (cf std::copy)
 opt::fill - optimised for trivial copy/small types (cf std::fill)
 opt::destroy_array - an example of optimisation based upon omitted
destructor calls
 opt::iter_swap - uses type_traits to determine whether the iterator is a
proxy
                  in which case it uses a "safe" approach, otherwise calls
swap
                  on the assumption that swap may be specialised for the
pointed-to type.

The results from using either Borland C++ Builder or gcc2.95 are broadly
similar, the output from the C++ Builder compiled version are given below.

Comments:

Neither gcc nor Builder were able to effectively eliminate empty loops in
the destructor test.
Optimisations involving memset gave a larger benefit than those involving
memcpy (which seem to be more commonly used).
call_traits is partly broken under C++ Builder (compiler bug) so the times
below are misleading for the fill<int*,int> test - the optimised form does
not in fact invoke the optimisation in this case! Under gcc a benefit was
observed (the times were 5.38 vs 8.07 micro seconds).

Program output:

Measuring times in micro-seconds per 1000 elements processed

testing destroy_array...
[Some compilers may be able to optimise the "unoptimised"
 versions as well as type_traits does.]
destroy_array<int>: 0.005
destroy_array<int>(unoptimised#1): 8.635
destroy_array<int>(unoptimised#2): 5.395

testing copy...
[Some standard library versions may already perform this optimisation.]
opt::copy<const int*, int*>: 2.525
std::copy<const int*, int*>: 8.13

testing fill(char)...
[Some standard library versions may already perform this optimisation.]
opt::fill<char*, char>: 1.416
std::fill<char*, char>: 8.159

testing fill(int)...
[Tests the effect of call_traits pass-by-value optimisation -
the value of this optimisation may depend upon hardware characteristics.]
opt::fill<int*, int>: 8.94 <- this is the spurious result (compiler
bug).
std::fill<int*, int>: 8.635

- John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk