Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,608 members, 7,958,845 topics. Date: Thursday, 26 September 2024 at 05:34 AM |
Nairaland Forum / Science/Technology / Programming / Java Rocks: Java Rules (1159 Views)
Read The Rules And Practices Of Extreme Programming Online For Free / 10 Rules I Followed To Teach Myself To Code (You Can Do This!) / Java Rules,who Thinks Otherwise? (2) (3) (4)
Java Rocks: Java Rules by javarules(m): 12:10pm On Jul 21, 2008 |
I came upon this post, and i think every java lovers (and java haters too ) should read this. The original post is at http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/ but that page takes a mighty long time to load, s if your con is slow (like mine) then I have taken the pain to copy, paste and make the article a lil bit intelligible for you, u dont have to go to that link Problem Statement Flavius Josephus was a roman historian of Jewish origin. During the Jewish-Roman wars of the first century AD, he was in a cave with fellow soldiers, 40 men in all, surrounded by enemy Roman troops. They decided to commit suicide by standing in a ring and counting off each third man. Each man so designated was to commit suicide…Josephus, not wanting to die, managed to place himself in the position of the last survivor. In the general version of the problem, there are n soldiers numbered from 1 to n and each k-th soldier will be eliminated. The count starts from the first soldier. What is the number of the last survivor Design I actually changed the design of the solution as compared to the original post. Instead of using the deeply recursive calls as used in the earlier post, I decided to split the logic into two classes, and use loop iteration instead of recursion. It is my belief that we tend to do loop iterations far more frequently than recursions, and the resultant class design having two classes - one to indicate a Chain and one reflecting a Person seemed more appropriate to me. Logic The Chain object contains a reference to one person (first) who is but one member in a circular linked list. Each person object has a reference to its previous (prev) and next (next) person in the circle. When the kill loop starts, it sets a threshold (nth). The count starts with 1 from the first person. Each person when asked to shout, checks if the shout count (shout) is less than the threshold (nth). If less, the person just returns an incremented count. If the two are same, the person in effect commits suicide. In doing so the person, updates the next reference of its prev, and prev reference of its next to take himself off the circle and keep the circle consistent, finally returning a shout of 1 (which is what the next person in the list will shout). The code does not have any comments (sorry!) and all the console outputs have been removed so that the benchmarking activity is not interfered with by the IO overheads. The results All the results are as observed on my notebook with the following config OS : Ubuntu Gutsy Gibbon 7.10 Kernel : 2.6.22-15-generic CPU : Intel® Core™ Duo CPU T2600 @ 2.16GHz RAM : 2GB Language Version Lines of Code Time per iteration (microseconds) Java Sun JDK 1.6.0.03 C++ 4.1.3 20070929 (prerelease) 86 3 (Ubuntu 4.1.2-16ubuntu2) Compiled with optimisation -O3 Ruby ruby 1.9.0 (2008-04-14 revision 16006) 63 [i686-linux] ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux] 372 380 jruby : ruby 1.8.6 (2008-05-28 rev 6586) [i386-jruby1.1.2] 84 80 Python 2.5.1 41 225 Jython 2.2.1 on JRE 1.6.0.03 41 884 Groovy Groovy Version: 1.5.6 81 363 JVM: 1.6.0_03-b05 uncompiled Compiled to bytecode and run using java 360 UpdateGroovy Version: 1.6-beta-1 JVM: 1.6.0_03 104 PHP PHP 5.2.3-1ubuntu6.3 (cli) 85 593 Updates : * Ken suggested syntactic improvements (see comments below) which lead to even faster ruby execution times : jruby : 80 microseconds, ruby 1.9 : 89 microseconds, ruby 1.8.6 : 380 microseconds. The table above has been updated * Cato requested a run using Groovy 1.6 beta 1 - have updated the same. Big improvement * Tim Fountain in a comment below indicates that on his hardware (core 2 Quad) with Ubuntu Hardy Heron, Ruby 1.8.6 (same version as above) performs somewhat faster (15%) whereas upgraded version of Python and PHP run much faster(63% for python and 83% for ruby). Another difference in config- he is running 64 bit. o python - version 2.5.2: - 138 microseconds o ruby - 1.8.6 (2007-09-24 patchlevel 111) [x86_64-linux] :321 microseconds o PHP - 5.2.4-2ubuntu5.1 with Suhosin-Patch 0.9.6.2 (cli): 323 microseconds. * Peter Lupo requested a reduction in line count for Java since the conventional way is to have the opening braces not on a separate independent line. Given the fact that it is a fair comment, I have reduced the line count of java to 86 (didn’t physically change the code - 86 = 101 - 15 opening curly braces). Summarisation The following are the results. Given the long code blocks, I am presenting the summarisation first followed by the code. Note: This can only be treated as one particular benchmark. The results are a little atypical with respect to my general understanding. Advise caution against drawing broad conclusions based on this benchmark alone but would suggest that you could treat this as one data point amongst many. People better versed than me in the details of language runtimes might be able to suggest why some of the results seem surprising or atypical. * Java / C++ Rock : The performance of Java and C++ was head and shoulders beyond other languages (nearly 100 times faster). My thought is that while a difference of 10x was only to be expected - this difference was just way too massive * Java is faster than C++ : Though I had read about other microbenchmarks reaching the same conclusions, it is the first time I actually ran one where Java was faster. There are many others I have run where C++ beats Java quite handsomely. More importantly - the performance of C++ worsened by almost 40% once I added code which started freeing memory that was being allocated (there’s still a small memory leak in the code - there is no Chain destructor which will clean up first). I would later definitely want to look at the impact of garbage collection in this context, and whether the Java garbage collector simply was much faster than the hand crafted new - delete calls in C++. * Ruby 1.9 is twice as fast as Python : While it has been known for a while Ruby 1.9 is much faster than Ruby 1.8.6, heres one more supporting data point. I was expecting ruby 1.9 to give python a run for its performance money. But at least in this particular context it seems to be much much faster. * JRuby is faster than Ruby : Even ruby 1.9. Very interesting indeed. * Jython still has some catching up to do : Though in the ballpark as the other languages, it was the slowest in the pack. * Overhead of dynamism is dominant : I have no idea if JRuby ran much faster because of the java bytecode or because of its implementation (though its performance was not even remotely close to that of Java). However even after I compiled groovy code, to java bytecode, it still ran much slower than python and ruby. It seems the overhead of supporting dynamic constructs is much more dominant than any benefits that one gets out of compilation (whether to java byte code or to intermediate compiled files). I think the argument that because something compiles to java bytecode it is likely to be fast should be looked at a little carefully. * PHP stays at the rear end : Though I benchmarked PHP for the first time, I wasn’t completely surprised by the fact that PHP could only manage to be faster than Jython. Update : There are many comments to this post including those from cwilbur who benchmarks perl using a idiomatic method, Paddy3118 who offers an optimised algorithm for python, and peter lawrey who offers an optimised algorithm for Java. I would like to state that each of their solutions offer superior performance than that what has been described here. However I believe any benchmark comparison should compare apples to apples. Should these contributions be taken into account and be reflected in the table above ? I certainly believe there is a case to do so as an exercise using a different algorithm. However to ensure that it is a fair comparison, one has to modify all the code in all other languages also to reflect the same algorithm. Only then can we get an apple to apples comparison. That is probably an exercise for another post. Is the algorithm I have chosen the fastest - No. However I believe it is a very readable algorithm and if one ignores the IO with networks and databases and files, it is probably close to the kind of code many programmers write (and maintain) on a day to day basis. It has been consistently implemented in all the languages. Readers should be aware that there are algorithms which will deliver much superior performance - but they will also make the performance superior in all the languages (perhaps to slightly differing extents and thus possibly somewhat different results). The code For all you who are either interested in running it for yourself or would like to perhaps explore this in more detail, , here’s the code. Note that I am not equally competent across all languages. So if you believe there is something that could be more appropriate way to code the same, do post a comment. One of the things I have tried to do is to ensure that the code remains more or less similar across all languages. Also I have used getter - setters or skipped them based on my understanding of the generally accepted convention for users of the language. Java : view plaincopy to clipboardprint? 1. package com.dnene.josephus; 2. 3. public class Chain 4. { 5. private Person first = null; 6. 7. public Chain(int size) 8. { 9. Person last = null; 10. Person current = null; 11. for (int i = 0 ; i < size ; i++) 12. { 13. current = new Person(i); 14. if (first == null) first = current; 15. if (last != null) 16. { 17. last.setNext(current); 18. current.setPrev(last); 19. } 20. last = current; 21. } 22. first.setPrev(last); 23. last.setNext(first); 24. } 25. 26. public Person kill(int nth) 27. { 28. Person current = first; 29. int shout = 1; 30. while(current.getNext() != current) 31. { 32. shout = current.shout(shout, nth); 33. current = current.getNext(); 34. } 35. first = current; 36. return current; 37. } 38. 39. public Person getFirst() 40. { 41. return first; 42. } 43. public static void main(String[] args) 44. { 45. int ITER = 100000; 46. long start = System.nanoTime(); 47. for (int i = 0 ; i < ITER ; i++) 48. { 49. Chain chain = new Chain(40); 50. chain.kill(3); 51. } 52. long end = System.nanoTime(); 53. System.out.println("Time per iteration = " + ((end - start) / (ITER )) + " nanoseconds." 54. } 55. } 56. package com.dnene.josephus; 57. 58. public class Person 59. { 60. int count; 61. private Person prev = null; 62. private Person next = null; 63. 64. public Person(int count) 65. { 66. this.count = count; 67. } 68. 69. public int shout(int shout, int deadif) 70. { 71. if (shout < deadif) return (shout + 1); 72. this.getPrev().setNext(this.getNext()); 73. this.getNext().setPrev(this.getPrev()); 74. return 1; 75. } 76. 77. public int getCount() 78. { 79. return this.count; 80. } 81. 82. public Person getPrev() 83. { 84. return prev; 85. } 86. 87. public void setPrev(Person prev) 88. { 89. this.prev = prev; 90. } 91. 92. public Person getNext() 93. { 94. return next; 95. } 96. 97. public void setNext(Person next) 98. 99. { 100. this.next = next; 101. } 102. } view plaincopy to clipboardprint? 1. package com.dnene.josephus; 2. 3. public class Chain 4. { 5. private Person first = null; 6. 7. public Chain(int size) 8. { 9. Person last = null; 10. Person current = null; 11. for (int i = 0 ; i < size ; i++) 12. { 13. current = new Person(i); 14. if (first == null) first = current; 15. if (last != null) 16. { 17. last.setNext(current); 18. current.setPrev(last); 19. } 20. last = current; 21. } 22. first.setPrev(last); 23. last.setNext(first); 24. } 25. 26. public Person kill(int nth) 27. { 28. Person current = first; 29. int shout = 1; 30. while(current.getNext() != current) 31. { 32. shout = current.shout(shout, nth); 33. current = current.getNext(); 34. } 35. first = current; 36. return current; 37. } 38. 39. public Person getFirst() 40. { 41. return first; 42. } 43. public static void main(String[] args) 44. { 45. int ITER = 100000; 46. long start = System.nanoTime(); 47. for (int i = 0 ; i < ITER ; i++) 48. { 49. Chain chain = new Chain(40); 50. chain.kill(3); 51. } 52. long end = System.nanoTime(); 53. System.out.println("Time per iteration = " + ((end - start) / (ITER )) + " nanoseconds." 54. } 55. } 56. package com.dnene.josephus; 57. 58. public class Person 59. { 60. int count; 61. private Person prev = null; 62. private Person next = null; 63. 64. public Person(int count) 65. { 66. this.count = count; 67. } 68. 69. public int shout(int shout, int deadif) 70. { 71. if (shout < deadif) return (shout + 1); 72. this.getPrev().setNext(this.getNext()); 73. this.getNext().setPrev(this.getPrev()); 74. return 1; 75. } 76. 77. public int getCount() 78. { 79. return this.count; 80. } 81. 82. public Person getPrev() 83. { 84. return prev; 85. } 86. 87. public void setPrev(Person prev) 88. { 89. this.prev = prev; 90. } 91. 92. public Person getNext() 93. { 94. return next; 95. } 96. 97. public void setNext(Person next) 98. 99. { 100. this.next = next; 101. } 102. } package com.dnene.josephus; public class Chain { private Person first = null; public Chain(int size) { Person last = null; Person current = null; for (int i = 0 ; i < size ; i++) { current = new Person(i); if (first == null) first = current; if (last != null) { last.setNext(current); current.setPrev(last); } last = current; } first.setPrev(last); last.setNext(first); } public Person kill(int nth) { Person current = first; int shout = 1; while(current.getNext() != current) { shout = current.shout(shout, nth); current = current.getNext(); } first = current; return current; } public Person getFirst() { return first; } public static void main(String[] args) { int ITER = 100000; long start = System.nanoTime(); for (int i = 0 ; i < ITER ; i++) { Chain chain = new Chain(40); chain.kill(3); } long end = System.nanoTime(); System.out.println("Time per iteration = " + ((end - start) / (ITER )) + " nanoseconds." } } package com.dnene.josephus; public class Person { int count; private Person prev = null; private Person next = null; public Person(int count) { this.count = count; } public int shout(int shout, int deadif) { if (shout < deadif) return (shout + 1); this.getPrev().setNext(this.getNext()); this.getNext().setPrev(this.getPrev()); return 1; } public int getCount() { return this.count; } public Person getPrev() { return prev; } public void setPrev(Person prev) { this.prev = prev; } public Person getNext() { return next; } public void setNext(Person next) { this.next = next; } } C++ view plaincopy to clipboardprint? 1. #include <stdio.h> 2. #include <stdlib.h> 3. #include <time.h> 4. #include <sys/time.h> 5. 6. class Person 7. { 8. 9. public: 10. 11. Person(int count) : _next(NULL), _prev(NULL) { _count = count; } 12. int shout(int shout, int nth) 13. { 14. if (shout < nth) return (shout + 1); 15. _prev->_next = _next; 16. 17. 18. _next->_prev = _prev; 19. return 1; 20. } 21. int count() { return _count; } 22. Person* next() { return _next; } 23. void next(Person* person) { this->_next = person ; } 24. Person* prev() { return _prev; } 25. void prev(Person* person) { this->_prev = person; } 26. private: 27. int _count; 28. Person* _next; 29. Person* _prev; 30. }; 31. 32. class Chain 33. { 34. public: 35. Chain(int size) : _first(NULL) 36. { 37. Person* current = NULL; 38. Person* last = NULL; 39. for(int i = 0 ; i < size ; i++) 40. { 41. current = new Person(i); 42. if (_first == NULL) _first = current; 43. if (last != NULL) 44. { 45. last->next(current); 46. current->prev(last); 47. } 48. last = current; 49. } 50. _first->prev(last); 51. last->next(_first); 52. } 53. Person* kill(int nth) 54. { 55. Person* current = _first; 56. int shout = 1; 57. while(current->next() != current) 58. { 59. Person* tmp = current; 60. shout = current->shout(shout,nth); 61. current = current->next(); 62. if (shout == 1) 63. { 64. delete tmp; 65. } 66. } 67. _first = current; 68. return current; 69. } 70. Person* first() { return _first; } 71. private: 72. Person* _first; 73. }; 74. 75. int main(int argc, char** argv) 76. { 77. int ITER = 1000000; 78. Chain* chain; 79. struct timeval start, end; 80. gettimeofday(&start,NULL); 81. for(int i = 0 ; i < ITER ; i++) 82. { 83. chain = new Chain(40); 84. chain->kill(3); 85. delete chain; 86. } 87. gettimeofday(&end,NULL); 88. fprintf(stdout,"Time per iteration = %d microseconds\n\r", (((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec)) / ITER); 89. //fprintf(stdout,"Last man standing is %d\n\r", (chain->first()->count() + 1)); 90. return 0; 91. } view plaincopy to clipboardprint? 1. #include <stdio.h> 2. #include <stdlib.h> 3. #include <time.h> 4. #include <sys/time.h> 5. 6. class Person 7. { 8. 9. public: 10. 11. Person(int count) : _next(NULL), _prev(NULL) { _count = count; } 12. int shout(int shout, int nth) 13. { 14. if (shout < nth) return (shout + 1); 15. _prev->_next = _next; 16. 17. 18. _next->_prev = _prev; 19. return 1; 20. } 21. int count() { return _count; } 22. Person* next() { return _next; } 23. void next(Person* person) { this->_next = person ; } 24. Person* prev() { return _prev; } 25. void prev(Person* person) { this->_prev = person; } 26. private: 27. int _count; 28. Person* _next; 29. Person* _prev; 30. }; 31. 32. class Chain 33. { 34. public: 35. Chain(int size) : _first(NULL) 36. { 37. Person* current = NULL; 38. Person* last = NULL; 39. for(int i = 0 ; i < size ; i++) 40. { 41. current = new Person(i); 42. if (_first == NULL) _first = current; 43. if (last != NULL) 44. { 45. last->next(current); 46. current->prev(last); 47. } 48. last = current; 49. } 50. _first->prev(last); 51. last->next(_first); 52. } 53. Person* kill(int nth) 54. { 55. Person* current = _first; 56. int shout = 1; 57. while(current->next() != current) 58. { 59. Person* tmp = current; 60. shout = current->shout(shout,nth); 61. current = current->next(); 62. if (shout == 1) 63. { 64. delete tmp; 65. } 66. } 67. _first = current; 68. return current; 69. } 70. Person* first() { return _first; } 71. private: 72. Person* _first; 73. }; 74. 75. int main(int argc, char** argv) 76. { 77. int ITER = 1000000; 78. Chain* chain; 79. struct timeval start, end; 80. gettimeofday(&start,NULL); 81. for(int i = 0 ; i < ITER ; i++) 82. { 83. chain = new Chain(40); 84. chain->kill(3); 85. delete chain; 86. } 87. gettimeofday(&end,NULL); 88. fprintf(stdout,"Time per iteration = %d microseconds\n\r", (((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec)) / ITER); 89. //fprintf(stdout,"Last man standing is %d\n\r", (chain->first()->count() + 1)); 90. return 0; 91. } #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/time.h> class Person { public: Person(int count) : _next(NULL), _prev(NULL) { _count = count; } int shout(int shout, int nth) { if (shout < nth) return (shout + 1); _prev->_next = _next; _next->_prev = _prev; return 1; } int count() { return _count; } Person* next() { return _next; } void next(Person* person) { this->_next = person ; } Person* prev() { return _prev; } void prev(Person* person) { this->_prev = person; } private: int _count; Person* _next; Person* _prev; }; class Chain { public: Chain(int size) : _first(NULL) { Person* current = NULL; Person* last = NULL; for(int i = 0 ; i < size ; i++) { current = new Person(i); if (_first == NULL) _first = current; if (last != NULL) { last->next(current); current->prev(last); } last = current; } _first->prev(last); last->next(_first); } Person* kill(int nth) { Person* current = _first; int shout = 1; while(current->next() != current) { Person* tmp = current; shout = current->shout(shout,nth); current = current->next(); if (shout == 1) { delete tmp; } } _first = current; return current; } Person* first() { return _first; } private: Person* _first; }; int main(int argc, char** argv) { int ITER = 1000000; Chain* chain; struct timeval start, end; gettimeofday(&start,NULL); for(int i = 0 ; i < ITER ; i++) { chain = new Chain(40); chain->kill(3); delete chain; } gettimeofday(&end,NULL); fprintf(stdout,"Time per iteration = %d microseconds\n\r", (((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec)) / ITER); //fprintf(stdout,"Last man standing is %d\n\r", (chain->first()->count() + 1)); return 0; } Python view plaincopy to clipboardprint? 1. class Person(object): 2. def __init__(self,count): 3. self.count = count; 4. self.prev = None 5. 6. 7. self.next = None 8. def shout(self,shout,deadif): 9. if (shout < deadif): return (shout + 1) 10. self.prev.next = self.next 11. self.next.prev = self.prev 12. return 1 13. 14. class Chain(object): 15. def __init__(self,size): 16. self.first = None 17. last = None 18. for i in range(size): 19. current = Person(i) 20. if self.first == None : self.first = current 21. if last != None : 22. last.next = current 23. current.prev = last 24. last = current 25. self.first.prev = last 26. last.next = self.first 27. def kill(self,nth): 28. current = self.first 29. shout = 1 30. while current.next != current: 31. shout = current.shout(shout,nth) 32. current = current.next 33. self.first = current 34. return current 35. 36. import time 37. ITER = 100000 38. start = time.time() 39. for i in range(ITER): 40. chain = Chain(40) 41. chain.kill(3) 42. end = time.time() 43. print 'Time per iteration = %s microseconds ' % ((end - start) * 1000000 / ITER) view plaincopy to clipboardprint? 1. class Person(object): 2. def __init__(self,count): 3. self.count = count; 4. self.prev = None 5. 6. 7. self.next = None 8. def shout(self,shout,deadif): 9. if (shout < deadif): return (shout + 1) 10. self.prev.next = self.next 11. self.next.prev = self.prev 12. return 1 13. 14. class Chain(object): 15. def __init__(self,size): 16. self.first = None 17. last = None 18. for i in range(size): 19. current = Person(i) 20. if self.first == None : self.first = current 21. if last != None : 22. last.next = current 23. current.prev = last 24. last = current 25. self.first.prev = last 26. last.next = self.first 27. def kill(self,nth): 28. current = self.first 29. shout = 1 30. while current.next != current: 31. shout = current.shout(shout,nth) 32. current = current.next 33. self.first = current 34. return current 35. 36. import time 37. ITER = 100000 38. start = time.time() 39. for i in range(ITER): 40. chain = Chain(40) 41. chain.kill(3) 42. end = time.time() 43. print 'Time per iteration = %s microseconds ' % ((end - start) * 1000000 / ITER) class Person(object): def __init__(self,count): self.count = count; self.prev = None self.next = None def shout(self,shout,deadif): if (shout < deadif): return (shout + 1) self.prev.next = self.next self.next.prev = self.prev return 1 class Chain(object): def __init__(self,size): self.first = None last = None for i in range(size): current = Person(i) if self.first == None : self.first = current if last != None : last.next = current current.prev = last last = current self.first.prev = last last.next = self.first def kill(self,nth): current = self.first shout = 1 while current.next != current: shout = current.shout(shout,nth) current = current.next self.first = current return current import time ITER = 100000 start = time.time() for i in range(ITER): chain = Chain(40) chain.kill(3) end = time.time() print 'Time per iteration = %s microseconds ' % ((end - start) * 1000000 / ITER) Ruby view plaincopy to clipboardprint? 1. class Person 2. attr_reader :count, :prev, :next 3. attr_writer :count, :prev, :next 4. 5. def initialize(count) 6. #puts 'Initializing person : ' + count.to_s() 7. @count = count 8. @prev = nil 9. @next = nil 10. end 11. 12. def shout(shout, deadif) 13. if shout < deadif 14. return shout + 1 15. end 16. @prev.next = @next 17. @next.prev = @prev 18. return 1 19. end 20. end 21. 22. class Chain 23. attr_reader :first 24. attr_writer :first 25. 26. def initialize(size) 27. @first = nil 28. last = nil 29. for i in (1, size) 30. current = Person.new(i) 31. if @first == nil 32. @first = current 33. end 34. if last != nil 35. last.next = current 36. current.prev = last 37. end 38. last = current 39. end 40. @first.prev = last 41. last.next = @first 42. end 43. 44. def kill(nth) 45. current = @first 46. shout = 1 47. while current.next != current 48. shout = current.shout(shout,nth) 49. current = current.next 50. end 51. @first = current 52. return current 53. end 54. end 55. 56. ITER=100000 57. start = Time.now 58. ITER.times { |i| 59. chain = Chain.new(40) 60. chain.kill(3) 61. } 62. ends = Time.now 63. puts 'Time per iteration = ' + ((ends - start) * 1000000 / ITER).to_s() + " microseconds" view plaincopy to clipboardprint? 1. class Person 2. attr_reader :count, :prev, :next 3. attr_writer :count, :prev, :next 4. 5. def initialize(count) 6. #puts 'Initializing person : ' + count.to_s() 7. @count = count 8. @prev = nil 9. @next = nil 10. end 11. 12. def shout(shout, deadif) 13. if shout < deadif 14. return shout + 1 15. end 16. @prev.next = @next 17. @next.prev = @prev 18. return 1 19. end 20. end 21. 22. class Chain 23. attr_reader :first 24. attr_writer :first 25. 26. def initialize(size) 27. @first = nil 28. last = nil 29. for i in (1, size) 30. current = Person.new(i) 31. if @first == nil 32. @first = current 33. end 34. if last != nil 35. last.next = current 36. current.prev = last 37. end 38. last = current 39. end 40. @first.prev = last 41. last.next = @first 42. end 43. 44. def kill(nth) 45. current = @first 46. shout = 1 47. while current.next != current 48. shout = current.shout(shout,nth) 49. current = current.next 50. end 51. @first = current 52. return current 53. end 54. end 55. 56. ITER=100000 57. start = Time.now 58. ITER.times { |i| 59. chain = Chain.new(40) 60. chain.kill(3) 61. } 62. ends = Time.now 63. puts 'Time per iteration = ' + ((ends - start) * 1000000 / ITER).to_s() + " microseconds" class Person attr_reader :count, :prev, :next attr_writer :count, :prev, :next def initialize(count) #puts 'Initializing person : ' + count.to_s() @count = count @prev = nil @next = nil end def shout(shout, deadif) if shout < deadif return shout + 1 end @prev.next = @next @next.prev = @prev return 1 end end class Chain attr_reader :first attr_writer :first def initialize(size) @first = nil last = nil for i in (1, size) current = Person.new(i) if @first == nil @first = current end if last != nil last.next = current current.prev = last end last = current end @first.prev = last last.next = @first end def kill(nth) current = @first shout = 1 while current.next != current shout = current.shout(shout,nth) current = current.next end @first = current return current end end ITER=100000 start = Time.now ITER.times { |i| chain = Chain.new(40) chain.kill(3) } ends = Time.now puts 'Time per iteration = ' + ((ends - start) * 1000000 / ITER).to_s() + " microseconds" Groovy view plaincopy to clipboardprint? 1. class Chain 2. { 3. def size 4. def first 5. 6. def init(siz) 7. { 8. def last 9. size = siz 10. for(def i = 0 ; i < siz ; i++) 11. { 12. def current = new Person() 13. current.count = i 14. if (i == 0) first = current 15. if (last != null) 16. { 17. last.next = current 18. } 19. current.prev = last 20. last = current 21. } 22. first.prev = last 23. last.next = first 24. } 25. 26. def kill(nth) 27. { 28. def current = first 29. def shout = 1 30. while(current.next != current) 31. { 32. shout = current.shout(shout,nth) 33. current = current.next 34. } 35. first = current 36. } 37. } 38. 39. class Person 40. { 41. def count 42. def prev 43. def next 44. 45. def shout(shout,deadif) 46. { 47. if (shout < deadif) 48. { 49. return (shout + 1) 50. } 51. prev.next = next 52. next.prev = prev 53. return 1 54. } 55. } 56. 57. def main(args) 58. { 59. println "Starting" 60. def ITER = 100000 61. def start = System.nanoTime() 62. for(def i = 0 ; i < ITER ; i++) 63. { 64. def chain = new Chain() 65. chain.init(40) 66. chain.kill(3) 67. } 68. def end = System.nanoTime() 69. println "Total time = " + ((end - start)/(ITER * 1000)) + " microseconds" 70. } 71. 72. def ITER = 100000 73. def start = System.nanoTime() 74. for(def i = 0 ; i < ITER ; i++) 75. { 76. def chain = new Chain() 77. chain.init(40) 78. chain.kill(3) 79. } 80. def end = System.nanoTime() 81. println "Time per iteration = " + ((end - start)/(ITER * 1000)) + " microseconds" view plaincopy to clipboardprint? 1. class Chain 2. { 3. def size 4. def first 5. 6. def init(siz) 7. { 8. def last 9. size = siz 10. for(def i = 0 ; i < siz ; i++) 11. { 12. def current = new Person() 13. current.count = i 14. if (i == 0) first = current 15. if (last != null) 16. { 17. last.next = current 18. } 19. current.prev = last 20. last = current 21. } 22. first.prev = last 23. last.next = first 24. } 25. 26. def kill(nth) 27. { 28. def current = first 29. def shout = 1 30. while(current.next != current) 31. { 32. shout = current.shout(shout,nth) 33. current = current.next 34. } 35. first = current 36. } 37. } 38. 39. class Person 40. { 41. def count 42. def prev 43. def next 44. 45. def shout(shout,deadif) 46. { 47. if (shout < deadif) 48. { 49. return (shout + 1) 50. } 51. prev.next = next 52. next.prev = prev 53. return 1 54. } 55. } 56. 57. def main(args) 58. { 59. println "Starting" 60. def ITER = 100000 61. def start = System.nanoTime() 62. for(def i = 0 ; i < ITER ; i++) 63. { 64. def chain = new Chain() 65. chain.init(40) 66. chain.kill(3) 67. } 68. def end = System.nanoTime() 69. println "Total time = " + ((end - start)/(ITER * 1000)) + " microseconds" 70. } 71. 72. def ITER = 100000 73. def start = System.nanoTime() 74. for(def i = 0 ; i < ITER ; i++) 75. { 76. def chain = new Chain() 77. chain.init(40) 78. chain.kill(3) 79. } 80. def end = System.nanoTime() 81. println "Time per iteration = " + ((end - start)/(ITER * 1000)) + " microseconds" class Chain { def size def first def init(siz) { def last size = siz for(def i = 0 ; i < siz ; i++) { def current = new Person() current.count = i if (i == 0) first = current if (last != null) { last.next = current } current.prev = last last = current } first.prev = last last.next = first } def kill(nth) { def current = first def shout = 1 while(current.next != current) { shout = current.shout(shout,nth) current = current.next } first = current } } class Person { def count def prev def next def shout(shout,deadif) { if (shout < deadif) { return (shout + 1) } prev.next = next next.prev = prev return 1 } } def main(args) { println "Starting" def ITER = 100000 def start = System.nanoTime() for(def i = 0 ; i < ITER ; i++) { def chain = new Chain() chain.init(40) chain.kill(3) } def end = System.nanoTime() println "Total time = " + ((end - start)/(ITER * 1000)) + " microseconds" } def ITER = 100000 def start = System.nanoTime() for(def i = 0 ; i < ITER ; i++) { def chain = new Chain() chain.init(40) chain.kill(3) } def end = System.nanoTime() println "Time per iteration = " + ((end - start)/(ITER * 1000)) + " microseconds" PHP view plaincopy to clipboardprint? 1. <? 2. class Person 3. { 4. function __construct($c) 5. { 6. $this->count = $c; 7. } 8. 9. 10. function getPrev() 11. { 12. return $this->prev; 13. } 14. 15. function setPrev($pr) 16. { 17. $this->prev = $pr; 18. } 19. 20. function getNext() 21. { 22. return $this->next; 23. } 24. 25. function setNext($nxt) 26. { 27. $this->next = $nxt; 28. } 29. 30. function shout($shout, $nth) 31. { 32. if ($shout < $nth) 33. { 34. return $shout + 1; 35. } 36. $this->getPrev()->setNext($this->getNext()); 37. $this->getNext()->setPrev($this->getPrev()); 38. return 1; 39. } 40. } 41. 42. class Chain 43. { 44. var $first; 45. 46. function __construct($size) 47. { 48. for($i = 0; $i < $size ; $i++) 49. { 50. $current = new Person($i); 51. if ($this->first == null) $this->first = $current; 52. if ($last != null) 53. { 54. $last->setNext($current); 55. $current->setPrev($last); 56. } 57. $last = $current; 58. } 59. $this->first->setPrev($last); 60. $last->setNext($this->first); 61. } 62. 63. function kill($nth) 64. { 65. $current = $this->first; 66. $shout = 1; 67. while($current->getNext() !== $current) 68. { 69. $shout = $current->shout($shout,$nth); 70. $current = $current->getNext(); 71. } 72. $this->first = $current; 73. } 74. } 75. 76. $start = microtime(true); 77. $ITER = 100000; 78. for($i = 0 ; $i < $ITER ; $i++) 79. { 80. $chain = new Chain(40); 81. $chain->kill(3); 82. } 83. $end = microtime(true); 84. printf("Time per iteration = %3.2f microseconds\n\r",(($end - $start) * 1000000 / $ITER)); 85. ?> view plaincopy to clipboardprint? 1. <? 2. class Person 3. { 4. function __construct($c) 5. { 6. $this->count = $c; 7. } 8. 9. 10. function getPrev() 11. { 12. return $this->prev; 13. } 14. 15. function setPrev($pr) 16. { 17. $this->prev = $pr; 18. } 19. 20. function getNext() 21. { 22. return $this->next; 23. } 24. 25. function setNext($nxt) 26. { 27. $this->next = $nxt; 28. } 29. 30. function shout($shout, $nth) 31. { 32. if ($shout < $nth) 33. { 34. return $shout + 1; 35. } 36. $this->getPrev()->setNext($this->getNext()); 37. $this->getNext()->setPrev($this->getPrev()); 38. return 1; 39. } 40. } 41. 42. class Chain 43. { 44. var $first; 45. 46. function __construct($size) 47. { 48. for($i = 0; $i < $size ; $i++) 49. { 50. $current = new Person($i); 51. if ($this->first == null) $this->first = $current; 52. if ($last != null) 53. { 54. $last->setNext($current); 55. $current->setPrev($last); 56. } 57. $last = $current; 58. } 59. $this->first->setPrev($last); 60. $last->setNext($this->first); 61. } 62. 63. function kill($nth) 64. { 65. $current = $this->first; 66. $shout = 1; 67. while($current->getNext() !== $current) 68. { 69. $shout = $current->shout($shout,$nth); 70. $current = $current->getNext(); 71. } 72. $this->first = $current; 73. } 74. } 75. 76. $start = microtime(true); 77. $ITER = 100000; 78. for($i = 0 ; $i < $ITER ; $i++) 79. { 80. $chain = new Chain(40); 81. $chain->kill(3); 82. } 83. $end = microtime(true); 84. printf("Time per iteration = %3.2f microseconds\n\r",(($end - $start) * 1000000 / $ITER)); 85. ?> |
Re: Java Rocks: Java Rules by candylips(m): 12:42pm On Jul 21, 2008 |
Doesn't prove jack IMHO . Performance benchmark comparisms are very subjective. You woudnt want to draw up conclusions from some wan n a be geek's lab experiment. |
Re: Java Rocks: Java Rules by javarules(m): 1:49pm On Jul 21, 2008 |
yeah i know, doesnt prove jack. but it doesnt have to prove jack, coz dts not what it was meant to prove, it was meant to prove the strenght of a language, two actually dt have stood the test of time did u see that jruby performs better than ruby? i wonder if its because of the J in it? ciao |
Re: Java Rocks: Java Rules by malone5923(m): 12:15am On Jul 22, 2008 |
I don't like long post so I didn't bother to read the contents(I will check it later though). Though you subject caught my attention and for some reason it reminds me of a UML class diagram |
Re: Java Rocks: Java Rules by javarules(m): 5:39pm On Jul 22, 2008 |
reading the first 5 to 10 lines is ok, dt will make u read d rest lol |
Re: Java Rocks: Java Rules by Ghenghis(m): 8:33pm On Jul 22, 2008 |
It might not prove much, but there are really lots of advancements in JIT compilation etc. especially since there are some optimisation hotspots due to the many iterations , You also have fast processors that the modern java compilers can optimize for automatically , i'm sure that amount of care was not given to C++ and other compiled languages, As per ruby,python and the rest , They're not know for runtime speed but ease and speed of development (the memory model of the language matters a lot here) , The algorithm is interesting,(not the conclusion or inference) but he should have used recusrion though, it would have been a simpler and more intuitive algorithm, Lets try that on for size (and leave the language wars) , , yeah java rules!!! |
Re: Java Rocks: Java Rules by alexis(m): 12:24pm On Jul 29, 2008 |
did u see that jruby performs better than ruby? i wonder if its because of the J in it? Ruby is single-threaded, java is multi-threaded hence it makes sense to compile ruby code to java class files. That is the primary reason guys use jruby. The ease and development speed of the ruby language combined with the scalability and robustness of the JVM. |
Re: Java Rocks: Java Rules by javarules(m): 5:40pm On Jul 30, 2008 |
All java lovers, this is a diression but forgive me, the JUG Nigeria has finally kicked off register and become an automatic member http://naijadukes.net/jand JAND is d place to be Strictly Naija, Strictly Java |
Re: Java Rocks: Java Rules by Mustay(m): 5:42pm On Jul 30, 2008 |
Java rules## java love Java prince chei! |
(1) (Reply)
Asp.net / Beginning Java Programming / Ahbeg It's Very Urgent... Who Could Help Me?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 93 |