Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <chrono>
- const int SIZE_VECTOR = 1000000;
- std::vector<long> vector(SIZE_VECTOR);
- /*
- Incializar el vector con valores desde 1 hasta SIZE_VECTOR
- */
- void initialize(int start, int end) {
- for (int i = start; i < end; ++i) {
- vector[i] = i;
- }
- }
- /*
- Elevar al cuadrado cada elemento del vector
- */
- void square(int start, int end) {
- for (int i = start; i < end; ++i) {
- vector[i] = vector[i] * vector[i];
- }
- }
- /*
- Sumar todos los elementos del vector
- */
- long sum(int start, int end) {
- long sum = 0;
- for (int i = start; i < end; ++i) {
- sum += vector[i];
- }
- return sum;
- }
- int main() {
- auto start_time = std::chrono::high_resolution_clock::now();
- initialize(0, SIZE_VECTOR/2);
- initialize(SIZE_VECTOR/2, SIZE_VECTOR);
- square(0, SIZE_VECTOR/2);
- square(SIZE_VECTOR/2, SIZE_VECTOR);
- long resultA = sum(0, SIZE_VECTOR/2);
- long resultB = sum(SIZE_VECTOR/2, SIZE_VECTOR);
- long result = resultA + resultB;
- std::cout << "Result: " << result << std::endl;
- auto end_time = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double> duration = end_time - start_time;
- std::cout << "\nTime sequential: " << duration.count() << " seconds" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement