All Projects → lnishan → Vector

lnishan / Vector

Licence: cc-by-4.0
➿ A supercharged std::vector implementation (minus Allocator)

Projects that are alternatives of or similar to Vector

Boomerang
Boomerang Decompiler - Fighting the code-rot :)
Stars: ✭ 265 (+124.58%)
Mutual labels:  clang, gcc, visual-studio
Pfr
std::tuple like methods for user defined types without any macro or boilerplate code
Stars: ✭ 896 (+659.32%)
Mutual labels:  clang, gcc, visual-studio
Ci helloworld
A simple example of how to setup a complete CI environment for C and C++
Stars: ✭ 357 (+202.54%)
Mutual labels:  clang, gcc, visual-studio
Croaring
Roaring bitmaps in C (and C++)
Stars: ✭ 735 (+522.88%)
Mutual labels:  clang, gcc, visual-studio
Moderncppci
This is an example of doing a Modern C++ project with CI
Stars: ✭ 109 (-7.63%)
Mutual labels:  clang, gcc, visual-studio
FrameOfReference
C++ library to pack and unpack vectors of integers having a small range of values using a technique called Frame of Reference
Stars: ✭ 36 (-69.49%)
Mutual labels:  visual-studio, gcc, clang
Sol2
Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
Stars: ✭ 2,791 (+2265.25%)
Mutual labels:  clang, gcc, visual-studio
ci playground
Playground for Cloud CI development for C++
Stars: ✭ 23 (-80.51%)
Mutual labels:  visual-studio, gcc, clang
Usrefl
Header-only, tiny (99 lines) and powerful C++20 static reflection library.
Stars: ✭ 287 (+143.22%)
Mutual labels:  clang, gcc
Uefi Ntfs
UEFI:NTFS - Boot NTFS partitions from UEFI
Stars: ✭ 386 (+227.12%)
Mutual labels:  gcc, visual-studio
Gcovr
generate code coverage reports with gcc/gcov
Stars: ✭ 482 (+308.47%)
Mutual labels:  clang, gcc
Efifs
EFI FileSystem drivers
Stars: ✭ 272 (+130.51%)
Mutual labels:  gcc, visual-studio
rooki
A stupid simple script runner supporting c, c++, rust, haskell and virtually anything
Stars: ✭ 26 (-77.97%)
Mutual labels:  gcc, clang
Libgenerics
libgenerics is a minimalistic and generic library for C basic data structures.
Stars: ✭ 42 (-64.41%)
Mutual labels:  data-structures, vector
Stdex
std C++ 11 library impementation with extra features using only C++ 98 and POSIX threads
Stars: ✭ 43 (-63.56%)
Mutual labels:  gcc, visual-studio
Ccache
ccache – a fast compiler cache
Stars: ✭ 1,128 (+855.93%)
Mutual labels:  clang, gcc
Avalonstudio
Cross platform IDE and Shell
Stars: ✭ 1,132 (+859.32%)
Mutual labels:  clang, gcc
Cvise
Super-parallel Python port of the C-Reduce
Stars: ✭ 77 (-34.75%)
Mutual labels:  clang, gcc
Graphical Debugging
GraphicalDebugging extension for Visual Studio
Stars: ✭ 88 (-25.42%)
Mutual labels:  vector, visual-studio
cpp-compiler-options
Compilation options for different versions of Clang, GCC and MSVC. Provided a generator and different file formats (cmake, xmake, meson, premake5, bjam/b2, ...)
Stars: ✭ 19 (-83.9%)
Mutual labels:  gcc, clang

vector

💜 A supercharged std::vector implementation (minus Allocator).

Build Status

This is meant to show you why you should ditch C++ STLs when performance is critical.
lni::vector should always be faster or just as fast as other implementations.

Since the implementation is compliant with the current C++17 Working Draft (minus Allocator),
lni::vector should be a drop-in replacement for std::vector in most cases.

Just note that lni::vector can generate redundancies up to 3x the data size (4x total).
(Consider using shrink_to_fit() to remove redundancies, but beware that a memory reallocation would take place.)

Usage

A very simple sample is provided below.

For details, refer to tester.cpp or an online reference.

#include "vector.hpp"

int main() {
	int i;
	lni::vector<int> v1;
	for (i = 0; i < 10000000; ++i)
		v1.push_back(i);
	for (auto &n: v1)
	 	printf("%d ", n);

	return 0;
}

Test Results

lni::vector is tested with all major compilers (gcc 6, clang 3.8 and VS14).
I've also included some sample test benches and a simple test script.

Current Benches

  • back_insertion
  • insertion
  • array_op
  • stack

Bench Usage

cd bench
./test.sh {Bench Name}

Bench Results

back_insertion


Hardware: 50GB Vmware Disk, 4GB RAM, 1 vCore (Guest VM, Host: 1TB SSHD, 16GB RAM, i7-3770)
Environment: gcc 6.1.1, Kubuntu 16.04 LTS

* std::vector
0.364s

* lni::vector
0.189s

* folly::fbvector
0.379s

92 - 100% faster


Hardware: 120GB SSD, 16GB RAM, i7-3770 (Desktop)
Environment: Visual Studio 2015, Windows 10 Enterprise 64-bit

* std::vector
0.651s

* lni::vector
0.261s

149% faster


Hardware: 16GB Persistent Disk, 3.75GB RAM, 1 HyperThread on 2.5GHz Xeon E5 v2 (Google Compute Engine n1-standard-1)
Environment: gcc 6.1.1, Ubuntu 16.04 LTS

* std::vector
0.599s

* lni::vector
0.281s

113% faster


Hardware: 16GB Persistent Disk, 3.75GB RAM, 1 HyperThread on 2.5GHz Xeon E5 v2 (Google Compute Engine n1-standard-1)
Environment: clang 3.8.0, Ubuntu 16.04 LTS

* std::vector
0.443s

* lni::vector
0.253s

75% faster


Hardware: 256GB SSD, 16GB RAM, i5-4260U (MacBook Air Early 2014)
Environment: gcc 6.1.0, OS X El Capitan

* std::vector
0.832s

* lni::vector
0.457s

82% faster


Hardware: 256GB SSD, 16GB RAM, i5-4260U (MacBook Air Early 2014)
Environment: clang-703.0.31, OS X El Capitan

* std::vector
0.789s

* lni::vector
0.487s

62% faster

Discussion

Why is lni::vector faster?

Fact 1. A LOT of people misuse std::vector.

Fact 2. Memory copying is expensive.

Most std::vector implementations are written as Dynamic Table.
The growth factors for these implementations are usually no greater than 2.
Also, the initial reserved sizes are usually assigned only 1.

This means:

Memory reallocations are very likely to take place, especially in the beginning.

  • You usually need more than 1 single space
  • Low growth factor and low initial reserved size means small sizes in the beginning (1, 2, 4, 8 ...), even though it grows fast later on

Assuming the growth factor is 2, the average cost of inserting a new element is 3, which is reasonably high.

  • Use your favorite complexity analysis method: Aggregation, Accounting, Potential ... etc., any will do
  • The average cost for lni::vector is 2.333, and is in fact lower for the reason stated below

The impact of growth factors is underestimated.

  • In these analyses, we "falsely" assume that a memory reallocation would be the last operation
  • The actual average cost is in fact lower, because the costs for the elements inserted after the last memory reallocation are essentially FREE

The correct way to use your vector

If you still want to use STLs,
ALWAYS reserve a reasonable size before you start inserting elements.

A better way in my opinion, is to:
Use an implementation with a high growth factor like mine,
and use shrink_to_fit() remove redundancies after you've completed inserting all the elements.
This is because you don't always know how much space you need (Sometimes it depends on the input),
and over-reserving isn't always a good thing.

License

Creative Commons Attribution 4.0 International

lni::vector by Jasmine "lnishan" Chen is licensed under a Creative Commons Attribution 4.0 International License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].