Advertisement
PavloPonomarov

VectorVersusHash

Jan 28th, 2020
2,936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "vectorfighter.h"
  2.  
  3. VectorFighter::VectorFighter()
  4. {
  5.     vector.reserve(SIZE);
  6. }
  7.  
  8. void VectorFighter::fillVector(double *values)
  9. {
  10.     timer.start();
  11.     for(int i = 0; i < SIZE; i++){
  12.         vector.append(values[i]);
  13.     }
  14.     qDebug()<<"Vector write elapsed time: "<<timer.nsecsElapsed();
  15. }
  16.  
  17. void VectorFighter::dumpVector()
  18. {
  19.     timer.start();
  20.     double *temp = new double[SIZE];
  21.     for(int i = 0; i < vector.count(); i++){
  22.         temp[i] = vector[i];
  23.     }
  24.     qDebug()<<"Vector read elapsed time: "<<timer.nsecsElapsed();
  25. }
  26. //---------------------------------------------------------------------------------------------
  27. #include "hashfighter.h"
  28.  
  29. HashFighter::HashFighter()
  30. {
  31.     hash.reserve(SIZE);
  32. }
  33.  
  34. void HashFighter::fillHash(double *values)
  35. {
  36.     timer.start();
  37.     for(int i = 0; i < SIZE; i++){
  38.         hash.insert(i, values[i]);
  39.     }
  40.     qDebug()<<"Hash write elapsed time: "<<timer.nsecsElapsed();
  41. }
  42.  
  43. void HashFighter::dumpHash()
  44. {
  45.     timer.start();
  46.     QList<int> keys = hash.keys();
  47.     double *temp = new double[SIZE];
  48.     for(int i = 0; i < keys.count(); i++){
  49.         temp[i] = hash[keys[i]];
  50.     }
  51.     qDebug()<<"Hash read elapsed time: "<<timer.nsecsElapsed();
  52. }
  53. //---------------------------------------------------------------------------------------------
  54. #include <vectorfighter.h>
  55. #include <hashfighter.h>
  56. #include <cstdlib>
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60.     VectorFighter *vector = new VectorFighter();
  61.     HashFighter *hash = new HashFighter();
  62.     double *values = new double[1000];
  63.     for(int i = 0; i < 1000; i++){
  64.         values[i] = rand()%100 / rand()%99;
  65.     }
  66.     vector->fillVector(values);
  67.     vector->dumpVector();
  68.     hash->fillHash(values);
  69.     hash->dumpHash();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement