DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Valarray Vs. Array Matrix-calculation Performance

Snippets Manager user avatar by
Snippets Manager
·
Jan. 17, 07 · · Code Snippet
Like (0)
Save
Tweet
699 Views

Join the DZone community and get the full member experience.

Join For Free
Demonstrates the usage of valarray and measures the performance of some arithmetic operations on matriced to be compared with the same operations performed with ordinary arrays. 


#include 
#include 
#include 
#include 
#include 
using namespace std;

void rndm(float& a);
void zerocheck(float& a);
double duration(vector::iterator start, vector::iterator stop);

int main()
{
  int i(0);
  int p(0);
  float k(5.3235),l(9.212);
  int size(4000000);

  vector vstart;
  vector vstop;
  vector::iterator istartclock;
  vector::iterator istopclock;
  vector vfloat(size);
  vector::iterator ifloat;
  valarray valfloat(size);

  cout << "Filling float array with " << size << " random elements" << endl;
  cout << "(each element needs " << sizeof(float) << " bytes of memory, so " \
       << "we need to make use of " << size*sizeof(float)/1000000<< "MBytes.)"<< endl;

  // ======================= ORDINARY OPERATIONS =======================
  // fill array

  vstart.push_back(clock());
  //  vfloat.resize(size);
  ifloat=vfloat.begin();
  for_each(vfloat.begin(),vfloat.end(),rndm);
  vstop.push_back(clock());

  cout << "****** Phase 1: Math. operations on ordinary array" << endl;  

  // Add k to each element
  cout << "*** Adding " << k << " to each element " ;
  cout << "(10th element was: " << *(ifloat+10) ;
  vstart.push_back(clock());
  for (i=0; i::iterator start, 
	      vector::iterator stop
	      )
{
  return difftime(*stop,*start) ;
}

Data structure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Most Popular Kubernetes Alternatives and Competitors
  • 6 Things Startups Can Do to Avoid Tech Debt
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • How to Submit a Post to DZone

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo