Skip to content

Instantly share code, notes, and snippets.

@mroman42
Created June 4, 2014 11:30
Show Gist options
  • Save mroman42/fac324fb9d705c7d7ca4 to your computer and use it in GitHub Desktop.
Save mroman42/fac324fb9d705c7d7ca4 to your computer and use it in GitHub Desktop.
DAXPY para práctica de AC.
#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <random>
#include <sstream>
#include <vector>
int main(int argc, char *argv[])
{
if (argc != 2)
throw std::invalid_argument("necesito el tamaño del vector");
size_t n;
std::istringstream iss(argv[1]);
iss >> n;
if(!iss)
throw std::invalid_argument("tamaño de vector no válido");
std::vector<int> x(n);
std::vector<int> y(n);
std::default_random_engine generator(n);
std::uniform_int_distribution<int> distribution(0, 9);
auto rng = std::bind(distribution, generator);
for (unsigned int i=0; i<n; i++) {
x[i] = rng();
y[i] = rng();
}
double a = rng();
auto start = std::chrono::high_resolution_clock::now();
for (unsigned int i=1; i<=n; i++)
y[i]= a*x[i] + y[i];
auto stop = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count() << "ns" << std::endl;
// Imprime para evitar que la optimización borre el código intermedio
if (n < 10)
std::cerr << "c = " << x[1] << y[1] << x[n-1] << y[n-1] << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment