Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 21st, 2005, 5:05 AM   #11
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by OpenLoop
Didn't know that Python compiles, but it still slow compared to Java and too slow compared to C++. See my thread where i made a small comparison
Thanks for the info and the GCJ link.
Using xrange instead of range speeds up the python implementation by about 40%, but yep, Python is certainly not the language to use for low level stuff! That said, Python gets around this by making it relatively simple to write modules in C or C++, so in theory, any low level, CPU-intensive code can be farmed out to C. However, this has the disadvantage of tying Python to one platform.

This benchmarking stuff looks rather interesting, so I benchmarked a few languages myself with the primes algorithm:
# GCC: primes.c
int main() {
  int n = 0, d = 0;
  for (n = 2; n < 50000; n++) {
    for (d = 2; d < n/2 + 1; d++) {
      if (n % d == 0) {
        break;
      }
    }
  }
}
# Time to run: 971 milliseconds

# Java 1.5.0
public class primes {
  public static void main(String[] args) {
    int n = 0, d = 0;
    for (n = 2; n < 50000; n++) {
      for (d = 2; d < n/2 + 1; d++) {
        if (n % d == 0) {
          break;
        }
      } 
    }
  }
}
# Time to run: 1098 milliseconds

# Python 2.4.1 (using range)
for n in range(2,50000):
    for d in range(2,n/2+1):
        if (n % d == 0):
            break
# Time to run: 33346 milliseconds

# Python 2.4.1 (using xrange)
for n in xrange(2,50000):
    for d in xrange(2,n/2+1):
        if (n % d == 0):
            break
# Time to run: 19900 milliseconds

# Perl 5.8.4
for ($n = 2; $n < 50000; $n++) {
	for ($d = 2; $d < $n / 2 + 1; $d++) {
		if ($n % $d == 0) {
			last;
		}
	}
}
# Time to run: 31798 milliseconds

# Nice 0.11.0 (using legacy for loops)
void main(String[] args) {
  for (int n = 2; n < 50000; n++) {
    for (int d = 2; d < n/2 + 1; d++) {
      if (n % d == 0) {
        break;
      }
    }
  }
}
# Time to run: 1041 milliseconds

# Nice 0.11.0 (using new for loops)
void main(String[] args) {
  for (int n : 2..50000)
    for (int d : 2..(n/2 + 1))
      if (n % d == 0)
        break;
}
# Time to run: 3752 milliseconds

# Ruby 1.8.2
for n in 2..50000
  for d in 2..(n/2 + 1)
    break if (n % d == 0)
  end
end
# Time to run: 92141 milliseconds
Arevos is offline   Reply With Quote
Old Aug 21st, 2005, 9:29 AM   #12
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Wow this xrange is powerful stuff. How come it was not mentionned in the Python tutorial. (or maybe i didn't get that far in the tutorial)
You should start a thread in the Python forums "range vs xrange" and let the Pythoners know the difference.
OpenLoop is offline   Reply With Quote
Old Aug 21st, 2005, 10:02 AM   #13
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
As I understand it, range returns a sequencial list of integers, whilst xrange returns an object that pretends to be a sequencial list of integers. So using 'range(50000)' in a for loop requires Python to create a list of 50'000 integers, whilst the xrange object just returns the integer needed.

If you want to think about it in Java terms, xrange might look something like:
class XRange implements List {
  private int n;
  XRange(int n) {
    this.n = n;
  }

  ...

  Iterator iterator() {
    return new Iterator(n) {
      private int i = 0;
      private int n = 0;
      Iterator(int n) {
        this.n = n;
      }
      boolean hasNext() {
        return i < n;
      }
      Object next() {
        i += 1
        return Integer(i - 1);
      }
      void remove() {}
    }
  }

  ...

}
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC