Advertisement
Guest User

C vs. Java

a guest
Jul 13th, 2011
2,806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. // courtesy of the incredible cliff click!
  2.  
  3. // javac bench.java; time java -server bench;
  4. // 1410065408
  5. // 0.056u 0.012s 0:00.09 66.6% 0+0k 0+64io 1pf+0w
  6.  
  7. // g++ -O bench.cpp; time a.out
  8. // 1410065408
  9. // 25.417u 0.012s 0:25.42 100.0% 0+0k 0+0io 0pf+0w
  10.  
  11. // Java code:
  12.  
  13. class B extends bench { int get() { return 1; } }
  14. class C extends bench { int get() { return 2; } }
  15.  
  16. abstract public class bench {
  17. abstract int get();
  18. static final int LEN=100000;
  19. public static void main(String args[]) {
  20. bench f = new B();
  21. int sum=0;
  22. for( int i=0; i<LEN; i++ )
  23. sum += f.test();
  24. System.out.println(sum);
  25. }
  26.  
  27. int test( ) {
  28. int sum=0;
  29. for( int i=0; i<LEN; i++ )
  30. sum += get();
  31. return sum;
  32. }
  33.  
  34. }
  35.  
  36. // C code:
  37.  
  38.  
  39. #include <stdio.h>
  40.  
  41. #define LEN 100000
  42. struct bench {
  43. virtual int get() = 0;
  44. int test( ) {
  45. int sum=0;
  46. for( int i=0; i<LEN; i++ )
  47. sum += get();
  48. return sum;
  49. }
  50. };
  51. class B : public bench { int get() { return 1; } };
  52. class C : public bench { int get() { return 2; } };
  53.  
  54. int main() {
  55. bench *f = new B();
  56. int sum=0;
  57. for( int i=0; i<LEN; i++ )
  58. sum += f->test();
  59. printf("%d\n",sum);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement