RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 388 | Replies: 4 | Thread Tools  Display Modes
Reply
Join Date: Feb 2008
Posts: 112
Reputation: sidatra79 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Containers Perfomance testing - memory error

  #1  
Jul 30th, 2008
Does anybody have any idea why the following code causes a memory error?
Thanks everybody in advance

  1. #include <stddef.h> // some older implementations lack <cstddef>
  2. #include <time.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. #include <vector>
  7. #include <algorithm>
  8. #include <list>
  9. #include <deque>
  10. #include <set>
  11.  
  12. #include <iostream>
  13. #include <iomanip>
  14.  
  15. typedef double element_t;
  16.  
  17. using namespace std;
  18.  
  19. vector<double> result_times; // results are puched into this vector
  20.  
  21. const char header[] =
  22. "\tarray"
  23. "\tvector with pointers"
  24. "\tvector with iterators"
  25. "\tdeque"
  26. "\tlist"
  27. "\tset"
  28. "\tmultiset";
  29.  
  30. void do_head()
  31. {
  32. cout << "size" << header << '\n';
  33. }
  34.  
  35. int do_tail()
  36. {
  37. // in case you want to stop for confirmation in a windows console application
  38. //char c;
  39. //cin >> c;
  40. return 0;
  41. }
  42.  
  43. void do_row(int size)
  44. {
  45. cout << size;
  46. cout << fixed << setprecision(2);
  47. for (size_t i = 0; i < result_times.size(); ++i)
  48. cout << '\t' << result_times[i];
  49. cout << '\n';
  50. }
  51.  
  52.  
  53. clock_t start_time;
  54.  
  55. inline void start_timer() { start_time = clock(); }
  56.  
  57. inline double timer()
  58. {
  59. clock_t end_time = clock();
  60. return (end_time - start_time)/double(CLOCKS_PER_SEC);
  61. }
  62.  
  63. typedef void(*Test)(element_t*, element_t*, int);
  64. void run(Test f, element_t* first, element_t* last, int number_of_times)
  65. {
  66. start_timer();
  67. while (number_of_times-- > 0) f(first,last,number_of_times);
  68. result_times.push_back(timer());
  69. }
  70.  
  71. void array_test(element_t* first, element_t* last, int number_of_times)
  72. {
  73. element_t* array = new element_t[last - first];
  74. copy(first, last, array);
  75. sort(array, array + (last - first));
  76. unique(array, array + (last - first));
  77. delete [] array;
  78. }
  79.  
  80. void vector_pointer_test(element_t* first, element_t* last, int number_of_times)
  81. {
  82. vector<element_t> container(first, last);
  83. // &*container.begin() gets us a pointer to the first element
  84. sort(&*container.begin(), &*container.end());
  85. unique(&*container.begin(), &*container.end());
  86. }
  87.  
  88. void vector_iterator_test(element_t* first, element_t* last, int number_of_times)
  89. {
  90. vector<element_t> container(first, last);
  91. sort(container.begin(), container.end());
  92. unique(container.begin(), container.end());
  93. }
  94.  
  95. void deque_test(element_t* first, element_t* last, int number_of_times)
  96. {
  97. // deque<element_t> container(first, last); CANNOT BE USED BECAUSE OF MVC++ 6
  98. deque<element_t> container(size_t(last - first), 0.0);
  99. copy(first, last, container.begin());
  100. sort(container.begin(), container.end());
  101. unique(container.begin(), container.end());
  102. }
  103.  
  104. void list_test(element_t* first, element_t* last, int number_of_times)
  105. {
  106. list<element_t> container(first, last);
  107. container.sort();
  108. container.unique();
  109. }
  110.  
  111. void set_test(element_t* first, element_t* last, int number_of_times)
  112. {
  113. set<element_t> container(first, last);
  114. }
  115.  
  116. void multiset_test(element_t* first, element_t* last, int number_of_times)
  117. {
  118. multiset<element_t> container(first, last);
  119. typedef multiset<element_t>::iterator iterator;
  120. {
  121. iterator first = container.begin();
  122. iterator last = container.end();
  123.  
  124. while (first != last) {
  125. iterator next = first;
  126. if (++next == last) break;
  127. if (*first == *next)
  128. container.erase(next);
  129. else
  130. ++first;
  131. }
  132. }
  133. }
  134.  
  135. void initialize(element_t* first, element_t* last)
  136. {
  137. element_t value = 0.0;
  138. while (first != last) {
  139. *first++ = value;
  140. value += 1.;
  141. }
  142. }
  143.  
  144. double logtwo(double x)
  145. {
  146. return log(x)/log((double) 2.0);
  147. }
  148.  
  149. const int largest_size = 1000000;
  150.  
  151. int number_of_tests(int size) {
  152. double n = size;
  153. double largest_n = largest_size;
  154. return int(floor((largest_n * logtwo(largest_n)) / (n * logtwo(n))));
  155. }
  156.  
  157. void run_tests(int size)
  158. {
  159. const int n = number_of_tests(size);
  160. const size_t length = 2*size;
  161. result_times.clear();
  162.  
  163. // make a random test set of the chosen size:
  164. vector<element_t> buf(length);
  165. element_t* buffer = &buf[0];
  166. element_t* buffer_end = &buf[length];
  167. initialize(buffer, buffer + size); // elements
  168. initialize(buffer + size, buffer_end); // duplicate elements
  169. random_shuffle(buffer, buffer_end);
  170.  
  171. // test the containers:
  172. run(array_test, buffer, buffer_end, n);
  173. run(vector_pointer_test, buffer, buffer_end, n);
  174. run(vector_iterator_test, buffer, buffer_end, n);
  175. run(deque_test, buffer, buffer_end, n);
  176. run(list_test, buffer, buffer_end, n);
  177. run(set_test, buffer, buffer_end, n);
  178. run(multiset_test, buffer, buffer_end, n);
  179. do_row(size);
  180. }
  181.  
  182. int main()
  183. {
  184. do_head();
  185. const int sizes [] = {10, 100, 1000, 10000, 100000, 1000000};
  186. const int n = sizeof(sizes)/sizeof(int);
  187. for (int i = 0; i < n; ++i) run_tests(sizes[i]);
  188. return do_tail();
  189. }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 51
Reputation: Kob0724 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Containers Perfomance testing - memory error

  #2  
Jul 30th, 2008
Holy cow kid. Can you give us a little more to work on then that? What error are you getting?
Reply With Quote  
Join Date: Dec 2005
Posts: 4,215
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 26
Solved Threads: 493
Colleague
Salem's Avatar
Salem Salem is offline Offline
Industrious Poster

Re: Containers Perfomance testing - memory error

  #3  
Jul 30th, 2008
As well as telling us what the error(s) are, also tell us which OS and compiler you're using.
Your comments make mention in several places about breakage with specific implementations.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: Feb 2008
Posts: 112
Reputation: sidatra79 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: Containers Perfomance testing - memory error

  #4  
Jul 31st, 2008
Hi everybody and sorry for the delay but I had to do some things.
Now concerning your questions:

1. I am using Visual studio 2008 and Xp
2. The error I get is the following: "Microsoft visual studio C Runtime library has detected a fatal error in ....exe"

Now I think I have to clarify some things concerning this code:

I am working on a project at the moment that requires code with a very fast execution time. In addition, there is a big amount of user defined data which has to be stored somewhere efficiently concerning always the aforementioned execution speed issue.

Since I am not a so experienced programmer, before I go on, with designing the required interfaces and implementations I decided to give a try on different data structures to see which one of them is the fastest. During that process I found this code on the net which looked to me like sth that could spare me some time on deciding which data structure to use.

This code was written originally from Alex Stepanov and Bjarne Stroustrup.... back in 1992. Unfortunately, like you already know it does'nt run (or at least I cannot bring it to run).

I would really appreciate if you could help find why it does'nt run successfully.

I think this code could reveal us all in praxis which container is the fastest and could be helpful for all the forum users. Theoretically anybody could claim that STL containers are faster or the opposite ors th else. But why not see it in praxis.

Thansk again for your time.
Reply With Quote  
Join Date: Apr 2008
Posts: 127
Reputation: ivailosp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 21
ivailosp ivailosp is offline Offline
Junior Poster

Re: Containers Perfomance testing - memory error

  #5  
Jul 31st, 2008
element_t* buffer_end = &buf[length-1];
btw. this is not working on VS2008
// &*container.begin() gets us a pointer to the first element
but it work on gcc
Last edited by ivailosp : Jul 31st, 2008 at 7:13 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:12 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC