Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Python is now the most popular introductory teaching language at top U.S. universities

  • 1



Python is currently the most popular language for teaching introductory computer science courses at top-ranked U.S. departments. Specifically, 8 of the top 10 CS departments (80%), and 27 of the top 39 (69%), teach Python in introductory CS0 or CS1 courses.



Source : Python is now the most popular introductory teaching language at top U.S. universities | blog@CACM | Communications of the ACM

Python computation speed....Once again...

  • 0
Just come across this video on youtube.
I was amazed by the difference in computation time between C++ and Python. I tried to do the same in my PC and compared with what I can obtain using the numpy library. Below the result.

#! /usr/bin/python2.7
from datetime import datetime

flips = 4000000

def regularPython():
    import random
    heads = 0

    for i in range(flips):
        if random.randint(0,1) == 0:
            heads+=1

    tails = flips-heads
    print 'heads=',heads
    print 'tails=',tails

def numpyVersion1():
    import numpy as np
    heads = 0

    for i in range(flips):
        if np.random.random_integers(0,1) == 0:
            heads+=1

    tails = flips-heads
    print 'heads=',heads
    print 'tails=',tails

def numpyVersion2():
    import numpy as np
    
    randomArray = np.random.random_integers(0,1,flips)

    heads = len(randomArray[randomArray==0])

    tails = flips-heads
    print 'heads=',heads
    print 'tails=',tails


if __name__=="__main__":
    t_start = datetime.now()
    regularPython()
    t_end = datetime.now()
    print "Total time: ", (t_end-t_start)
    t_start = datetime.now()
    numpyVersion1()
    t_end = datetime.now()
    print "Total time: ", (t_end-t_start)
    numpyVersion2()
    t_end = datetime.now()
    print "Total time: ", (t_end-t_start)


Here the output:

heads= 2001161
tails= 1998839
Total time:  0:00:06.139672
heads= 1999621
tails= 2000379
Total time:  0:00:01.597668
heads= 1999881
tails= 2000119
Total time:  0:00:01.707101


Python vs C++ Performance

  • 0
Just for curiosity, I wanted to see how fast is C++ compared with regular Python and Python using the numpy library. Below the results:

A bit of Python code with regular python, same loop using numpy, and numpy vectorial (like in MatLab):

'''
Created on Nov 19, 2012

@author: leal
'''

import numpy as np
from datetime import datetime
nSpectra = 12 * 32 * 256
nBinsTotal = nSpectra * 512;
    
def testSpectraIN5(): total = [] for i in range(nBinsTotal): res = 500 * 500 + i total.append(res) def testSpectraIN5np(): total = np.empty(nBinsTotal, dtype=float) for i in range(nBinsTotal): res = 500 * 500 + i total[i]=res def testSpectraIN5npOpt(): # res = [0..nBinsTotal-1] res = np.arange(nBinsTotal, dtype=float) total = 500 * 500 + res if __name__ == '__main__': print "Main has started!" print "* testSpectraIN5" t_start = datetime.now() testSpectraIN5() t_end = datetime.now() t_total = t_end - t_start print "Total time: ", t_total, " seconds" print "* testSpectraIN5np" t_start = datetime.now() testSpectraIN5np() t_end = datetime.now() t_total = t_end - t_start print "Total time: ", t_total, " seconds" print "* testSpectraIN5npOpt" t_start = datetime.now() testSpectraIN5npOpt() t_end = datetime.now() t_total = t_end - t_start print "Total time: ", t_total, " seconds" print "Main has finished!"

Python v 2.7.3 results:

Main has started!
* testSpectraIN5
Total time:  0:00:12.781883  seconds
* testSpectraIN5np
Total time:  0:00:09.553932  seconds
* testSpectraIN5npOpt
Total time:  0:00:00.823714  seconds
Main has finished!

For C++, I just did two cycles, one without initialising the vector with its size, and other one with it. Code is below:

// constructing vectors
#include <iostream>
#include <vector>
#include <valarray>

// C++11
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds milliseconds;

int main() {

    std::cout << "Main has started!" << std::endl;

    Clock::time_point t0 = Clock::now();

    int nSpectra = 12 * 32 * 256;
    int nBinsTotal = nSpectra * 512;

    std::vector<double> total; //(nBinsTotal);

    for (int i = 0; i < nBinsTotal; i++) {
        double res = 500 * 500 + i;
        total.push_back(res);
    }

    Clock::time_point t1 = Clock::now();
    milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);

    std::cout.precision(2);
    std::cout << "Total time: " << std::fixed << ms.count() * 1e-3 << " seconds"<< std::endl;

    std::cout << "Vector with known size:"<< std::endl;
    t0 = Clock::now();
    std::vector<double> total2(nBinsTotal);

    for (int i = 0; i < nBinsTotal; i++) {
        double res = 500 * 500 + i;
        total2[i] = res;
    }

    t1 = Clock::now();
    ms = std::chrono::duration_cast<milliseconds>(t1 - t0);

    std::cout.precision(2);
    std::cout << "Total time: " << std::fixed << ms.count() * 1e-3 << " seconds"<< std::endl;


    std::cout << "Main has finished!" << std::endl;
    return 0;
}

The result is:

Main has started!
Total time: 1.70 seconds
Vector with known size:
Total time: 0.74 seconds
Main has finished!

It's interesting to see how the programming approach changes the results!
Surprisingly, Python, using a vectorial numpy approach, can be as fast as C++. It's not a surprise however, that an initlised array in C++ can be twice faster!

EDIT:

What about java???
So I tried first with ArrayList and Vector but they were so slow (more than 30 seconds), that I didn't even dare to add them below... I came up with this code, but doesn't really compare to what I did in C++.

import java.util.Vector;

public class VectorTest {

    public static void main(String[] args) {

        int nSpectra = 12 * 32 * 256;
        int nBinsTotal = nSpectra * 512;

        double[] total2 = new double[nBinsTotal];
        
        long startTime = System.nanoTime();
        // <E> Element type of Vector e.g. String, Integer, Object ...
        for (int i = 0; i < nBinsTotal; i++) {
            double res = 500 * 500 + i;
            total2[i] = res;
        }
        long endTime = System.nanoTime();
        long duration = endTime - startTime;
        System.out.println("Total time: " + duration * 1e-9);

    }
}

Start calculation with arrays and initilisation.
Total time: 0.073217508

Using regular arrays, Java can be as fast as C++ Vectors with predefined length.

Matplotlib is as simple as this!

  • 0
Imagine data you have a comma separated file (CSV) called data.csv:

'position', 'value'
1.00000000,19.0612177051
6.21052632,100.657317014
11.42105263,111.081064358
16.63157895,232.929497684
21.84210526,224.490699818
27.05263158,279.538416476
32.26315789,377.203193621
37.47368421,443.521259213
42.68421053,444.889816318
47.89473684,527.26112329
53.10526316,569.99383123
58.31578947,635.948010167
63.52631579,667.667229067
68.73684211,734.623416964
73.94736842,754.704089053
79.15789474,834.453817955
84.36842105,828.770429695
89.57894737,884.032674467
94.78947368,925.518393595
100.0,978.592947013

And you want to plot the data and do a linear fitting (polynomial fit of degree one). Matplotlib does it like this:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

# read CSV as a numpy array
data = mlab.csv2rec('data.csv')

# print CSV file headers
print data.dtype.names

# load collumns as vectors
data_x = data['position']
data_y = data['value']

# plot raw data
plt.plot(data_x,data_y,'o')

# fit data with a polynomial of degree 1: ax+b=0
a,b = np.polyfit(data_x, data_y, 1)
data_y_fitted = np.polyval([a, b], data_x)

#plot fitted data
plt.plot(data_x,data_y_fitted,'-')

plt.show()


Here the resulting Plot:


Let's make it prettier:


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

# read CSV as a numpy array
filePath = 'data.csv'
data = mlab.csv2rec(filePath)

# print CSV file headers
print data.dtype.names

# load collumns as vectors
data_x = data['position']
data_y = data['value']

# figure
fig = plt.figure('.: MatPlotLib Example :.')
fig.suptitle('Plot for file '+filePath, fontsize=12, fontweight='bold')

# Add my axis (I only have one, but I could have two, e.g., mirrored Y)
ax = fig.add_subplot(111)

# plot raw data
# I'm going to add some labels to add a legend after
ax.plot(data_x,data_y,'o', label='Raw data')
ax.set_xlabel(data.dtype.names[0])

# Now I want to have X axis with some Latex:
from matplotlib import rc
rc('text', usetex=True)
ax.set_ylabel(data.dtype.names[1]+' ($\\AA^{-1}$)')
rc('text', usetex=False)

# fit data with a polynomial of degree 1: ax+b=0
a,b = np.polyfit(data_x, data_y, 1)
data_y_fitted = np.polyval([a, b], data_x)

#plot fitted data
ax.plot(data_x,data_y_fitted,'-', label='Fitted data')

# Let's add the legend to the lower right corner
ax.legend(loc= 'lower right')

plt.show()

Et voilĂ  the result:


Simple isn't it?

Python threading for dummies

  • 0
Below an example I did for my office mate who wants to get his python code to run on several cores.
Pretty usefull for those who are just starting threads.

#!/usr/bin/python

'''
@author: Ricardo Leal
'''
import time
import threading
from datetime import datetime

# Lock
lock = threading.Lock()


def myfunc(i):
    ''' Function to be threaded '''
    print "Tread %d is doing NON critical stuff" % i
    time.sleep(0.5)
    
    lock.acquire()
    print "Tread %d is doing critical stuff"% i
    time.sleep(0.5)
    print "Tread %d has finished doing critical stuff" % i 
    lock.release()

if __name__ == '__main__':
    
    t_start = datetime.now()    
    
    thread_list = []

    for i in range(10):
        print "Launching thread: ", i
        t = threading.Thread(target=myfunc, args=(i,))
        # Put threads in a list        
        thread_list.append(t)
        
        # optional (rather than putting them on a list)
        # t.start()
    
    # Start all threads
    [x.start() for x in thread_list]

    print "Waiting for the threads to do their job!"
    # Wait for all of them to finish
    [x.join() for x in thread_list]
    
    print "Main: I haved waited for the threads to finish!"    
    
    t_end = datetime.now()
    t_total = t_end - t_start
    
    print "Total time: ", t_total
    print "Main has finished!"