double.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // spreadsort double sorting example.
  2. //
  3. // Copyright Steven Ross 2009-2014.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // See http://www.boost.org/libs/sort for library home page.
  9. #include <boost/sort/spreadsort/spreadsort.hpp>
  10. #include <time.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <string>
  16. #include <fstream>
  17. #include <iostream>
  18. using namespace boost::sort::spreadsort;
  19. #define DATA_TYPE double
  20. #define CAST_TYPE boost::int64_t
  21. //Pass in an argument to test std::sort
  22. //Note that this converts NaNs and -0.0 to 0.0, so that sorting results are
  23. //identical every time
  24. int main(int argc, const char ** argv) {
  25. size_t uCount,uSize=sizeof(DATA_TYPE);
  26. bool stdSort = false;
  27. unsigned loopCount = 1;
  28. for (int u = 1; u < argc; ++u) {
  29. if (std::string(argv[u]) == "-std")
  30. stdSort = true;
  31. else
  32. loopCount = atoi(argv[u]);
  33. }
  34. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  35. if (input.fail()) {
  36. printf("input.txt could not be opened\n");
  37. return 1;
  38. }
  39. double total = 0.0;
  40. std::vector<DATA_TYPE> array;
  41. input.seekg (0, std::ios_base::end);
  42. size_t length = input.tellg();
  43. uCount = length/uSize;
  44. //Using this to support compilers that don't support 64-bit constants
  45. CAST_TYPE exponent_mask = 0x7ff00000;
  46. exponent_mask <<= 32;
  47. CAST_TYPE top_exponent_bit = 0x40000000;
  48. top_exponent_bit <<= 32;
  49. //Run multiple loops, if requested
  50. for (unsigned u = 0; u < loopCount; ++u) {
  51. input.seekg (0, std::ios_base::beg);
  52. //Conversion to a vector
  53. array.resize(uCount);
  54. unsigned v = 0;
  55. while (input.good() && v < uCount) {
  56. input.read(reinterpret_cast<char *>(&(array[v])), uSize );
  57. //Checking for denormalized numbers
  58. if (!(float_mem_cast<DATA_TYPE, CAST_TYPE>(array[v]) & exponent_mask)) {
  59. //Make the top exponent bit high
  60. CAST_TYPE temp = top_exponent_bit |
  61. float_mem_cast<DATA_TYPE, CAST_TYPE>(array[v]);
  62. memcpy(&(array[v]), &temp, sizeof(DATA_TYPE));
  63. }
  64. //Testcase doesn't sort NaNs; they just cause confusion
  65. if (!(array[v] < 0.0) && !(0.0 < array[v]))
  66. array[v] = 0.0;
  67. ++v;
  68. }
  69. clock_t start, end;
  70. double elapsed;
  71. start = clock();
  72. if (stdSort)
  73. //std::sort(&(array[0]), &(array[0]) + uCount);
  74. std::sort(array.begin(), array.end());
  75. else
  76. //float_sort(&(array[0]), &(array[0]) + uCount);
  77. float_sort(array.begin(), array.end());
  78. end = clock();
  79. elapsed = static_cast<double>(end - start) ;
  80. std::ofstream ofile;
  81. if (stdSort)
  82. ofile.open("standard_sort_out.txt", std::ios_base::out |
  83. std::ios_base::binary | std::ios_base::trunc);
  84. else
  85. ofile.open("boost_sort_out.txt", std::ios_base::out |
  86. std::ios_base::binary | std::ios_base::trunc);
  87. if (ofile.good()) {
  88. for (unsigned v = 0; v < array.size(); ++v) {
  89. ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
  90. }
  91. ofile.close();
  92. }
  93. total += elapsed;
  94. array.clear();
  95. }
  96. if (stdSort)
  97. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  98. else
  99. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  100. return 0;
  101. }