fork download
  1. // Attached: HW_11
  2. // File: HW_11
  3. // Programmer: Elaine Torrez
  4. // Class: CMPR 121
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <thread>
  11. using namespace std;
  12.  
  13. void writeRoots();
  14. void writeSquares();
  15.  
  16. int main()
  17. {
  18. clock_t start;
  19. clock_t end;
  20. double runtimeWithoutThreads;
  21. double runtimeWithThreads;
  22.  
  23. cout << "main: startup\n\n";
  24.  
  25. start = clock();
  26.  
  27. cout << "Waiting for file thread\n";
  28. writeRoots();
  29. writeSquares();
  30.  
  31. end = clock();
  32.  
  33. runtimeWithoutThreads = static_cast<double>(end - start) / CLOCKS_PER_SEC;
  34.  
  35. cout << "\nmain: done\n";
  36. cout << "Runtime without threads = " << runtimeWithoutThreads << " seconds.\n\n";
  37.  
  38. start = clock();
  39.  
  40. cout << "main: startup\n\n";
  41. cout << "Waiting for file thread\n";
  42.  
  43. thread firstThread(writeRoots);
  44. thread secondThread(writeSquares);
  45.  
  46. firstThread.join();
  47. secondThread.join();
  48.  
  49. end = clock();
  50.  
  51. runtimeWithThreads = static_cast<double>(end - start) / CLOCKS_PER_SEC;
  52.  
  53. cout << "\nmain: done\n";
  54. cout << "Runtime with threads = " << runtimeWithThreads << " seconds.\n\n";
  55.  
  56. if (runtimeWithThreads < runtimeWithoutThreads)
  57. {
  58. cout << "The multithreaded version saved time.\n";
  59. }
  60. else
  61. {
  62. cout << "The multithreaded version did not save time.\n";
  63. }
  64.  
  65. return 0;
  66. }
  67.  
  68. void writeRoots()
  69. {
  70. ofstream outputFile;
  71. int number;
  72.  
  73. outputFile.open("roots.txt");
  74.  
  75. cout << "Writing 1,000,000 square roots to a file\n";
  76.  
  77. for (number = 1; number <= 1000000; number++)
  78. {
  79. outputFile << "The square root of " << number << " is "
  80. << sqrt(number) << endl;
  81. }
  82.  
  83. outputFile.close();
  84.  
  85. cout << "The square roots are ready.\n";
  86. }
  87.  
  88. void writeSquares()
  89. {
  90. ofstream outputFile;
  91. int number;
  92. long long square;
  93.  
  94. outputFile.open("squares.txt");
  95.  
  96. cout << "Squaring 1000000 numbers\n";
  97.  
  98. for (number = 1; number <= 1000000; number++)
  99. {
  100. square = static_cast<long long>(number) * number;
  101.  
  102. outputFile << number << " squared is " << square << endl;
  103. }
  104.  
  105. outputFile.close();
  106.  
  107. cout << "The squares are ready.\n";
  108. }
Success #stdin #stdout 0.26s 5316KB
stdin
Standard input is empty
stdout
main: startup

Waiting for file thread
Writing 1,000,000 square roots to a file
The square roots are ready.
Squaring 1000000 numbers
The squares are ready.

main: done
Runtime without threads = 0.109116 seconds.

main: startup

Waiting for file thread
Squaring 1000000 numbers
Writing 1,000,000 square roots to a file
The squares are ready.
The square roots are ready.

main: done
Runtime with threads = 0.146913 seconds.

The multithreaded version did not save time.