GithubHelp home page GithubHelp logo

timdockins / structvarray_experiment Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 148 KB

Code that I used to do a run-time experiment

License: GNU General Public License v2.0

C++ 95.03% Makefile 4.97%

structvarray_experiment's Introduction

structvarray_experiment

This is the C++ code that I used to do a run-time experiment for accessing data stored in structs vs accessing data stored in arrays. The data is a set of particles having mass, radius, 3D position vector, and 3D velocity vector - all double values. The question is whether it is faster, in terms of data access, to store this in an array of Particle structs having mass, radius, and two 3-element arrays, or to store in four arrays of doubles.

The assumption is that some function will need to access the data of all pairs of particles to calculate the gravitational forces.

Intuition

My intuition about computer architecture said the following:

  • There is little difference, in terms of compiled code, in addressing arrays vs structs.
  • There is a small difference in sizing on structs, given padding, but that's not an issue here.
  • Caching will cause a small difference in access speed

Experiment

The idea is to test the access of data for use in a calculation. So, the following steps are performed:

  1. generate text files containing data with 100, 1000, 5000, 10000, 25000, 50000 elements respectively.
  2. For each size, read in the data into the target data structures.
  3. Start timing the procedure.
  4. Provide the data from the structure as parameters to a function that performs some calculation using those parameters.
  5. Record the time it takes to perform step through the 'lower-left triangle' of the cross-product of particles to particles.
  6. Perform 3 - 5 for 35 episodes and take the average time.

Compiling and running

I compiled this code with a GNU C++ compiler. It's what I had on my Ubuntu 13.10 laptop. Your mileage may vary.

The files in this experiment consist of:

  • speedtest_struct.cpp - runs the test using an array of structures
  • speedtest_array.cpp - runs the test using a set of arrays
  • generateRandomParticles.cpp - generates random test data

If you have 'make', simply run that.

Otherwise:

g++ -std=gnu++11 generateRandomParticles.cpp -o generate g++ -o speedtest_array speedtest_array.cpp g++ -o speedtest_struct speedtest_struct.cpp

See the code for usage.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.