Project7-Faculty2
November 16th, 2009 by zss93- predicted number of hours to complete: 10
- actual number of hours to complete: 50
As Dr. Glenn showed us the other day, the performance of map() could be superior to other methods when trying for 100 times to create a list of the square root of 10,000 values. The other methods that competed against map() to comprehend this list were (taken from classpage linked above):
def for_function () : l = [] for v in xrange(s) : l.append(math.sqrt(v)) return l def list_comprehension_function () : return [math.sqrt(v) for v in xrange(s)] def map_function () : return map(math.sqrt, xrange(s)) def generator_function () : return list((math.sqrt(v) for v in xrange(s)))
Here are the actual results for running these functions:
""" Performance.py 2.6.2 (r262:71600, Jul 28 2009, 14:05:43) [GCC 4.2.2] for_function 0.82852602005 list_comprehension_function 0.580744028091 map_function 0.353476047516 generator_function 0.658768892288 Done. """
I wanted to know why map was faster, so I explored that a little bit. The map() function is faster because it is compiled into C code and then natively ran on the machine. Intuitively this means that it will run faster because we are porting the code to a lower level instead of interpreting it first and running then on a higher abstraction mechanism. The other three methods specified above to construct the equivalent list are only being interpreted and hence will run slower in most cases.
Under Python’s performance wiki (Loops section), I found some documentation about performance and loops:
If the body of your loop is simple, the interpreter overhead of the for loop itself can be a substantial amount of the overhead. This is where themap function is handy. You can think of map as a for moved into C code. The only restriction is that the “loop body” of map must be a function call.
After reading this paragraph above, I have decided to write a little method that uses map but I make map take a lambda:
def map_lambda_function () : return map(lambda x:math.sqrt(x), xrange(s))
I ran this against the other functions and I got the results:
Performance.py
2.6.2 (r262:71600, Jul 28 2009, 14:05:43)
[GCC 4.2.2]
for_function
1.85488390923
list_comprehension_function
1.26931381226
map_function
1.26931381226
map_lambda_function
2.04882907867
generator_function
1.48617100716
As shown above, the passing of a lambda (and not a regular function) to map makes it much less faster, and this case it was the slowest of all methods. I hope this will be beneficial to whoever reads it. Peace.
Introduction:
You might be thinking: the security of an algorithm depends on the programmer’s ability to write secure code. This statement is absolutely true and cannot be underestimated. Yet, I believe that an extensive number of secure tools and certain design features in a programming language will make a programmer’s life easier in securing his or her code. This article will try to focus on some interesting Python design features and tools (built-in functionality and provided standard libraries) and see how they affect the security of coding in Python, in general.
A brief history of security in Python:
The problems with the current model:
Programmer tools and libraries to enhance security:
The Python interpreter is written in C. Python extension modules are written in C (or something similar). If you find an unprotected buffer in this C code, you can possibly overflow this buffer. This can be used for nasty things like corrupting the stack and injecting malicious code. There is a reason why the Python sandbox (rexec and Bastion modules) was disabled in Python 2.3.
Sources:
predicted number of hours to complete: 3
actual number of hours to complete : 5
Voting.out:
http://blogs.utexas.edu/zss93/project-3-votingout
TestVoting.out:
http://blogs.utexas.edu/zss93/project-3-testvotingout
Description:
For any given number that is less than or equal to ten million, express that number as a sum of four primes (that is, find four primes that add up to it).
Misc:
Predicted numbers of hours to complete: 2.
Actual hours to complete:2.
Output:
Primes.out: http://blogs.utexas.edu/zss93/project-2-primesout/
TestPrimes.out: http://blogs.utexas.edu/zss93/project-2-testprimesout/
Programming:
- A design that runs all the tests and passes with a 100% score.
- Contains no duplicate code.
- Expresses and implements ideas that you wish to express and implement.
- Has a minimum number of classes and methods.