<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Steven Harms &#187; sdl</title>
	<atom:link href="http://www.sharms.org/blog/tag/sdl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sharms.org/blog</link>
	<description>Life, Linux and Technology</description>
	<lastBuildDate>Sun, 28 Aug 2011 18:02:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python and real time graphical analysis</title>
		<link>http://www.sharms.org/blog/2009/11/python-and-real-time-graphical-analysis/</link>
		<comments>http://www.sharms.org/blog/2009/11/python-and-real-time-graphical-analysis/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 21:44:27 +0000</pubDate>
		<dc:creator>sharms</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sdl]]></category>

		<guid isPermaLink="false">http://www.sharms.org/blog/?p=650</guid>
		<description><![CDATA[I have a camera which has a motor attached which I can rotate using a serial cable. I figured it would be fun to have this camera analyze the webcam shots and turn in any direction there was motion. I pulled out python and pygame, and created a prototype. Unfortunately, I can&#8217;t make python go [...]


Related posts:<ol><li><a href='http://www.sharms.org/blog/2009/06/python-threads/' rel='bookmark' title='Permanent Link: Python threads'>Python threads</a></li>
<li><a href='http://www.sharms.org/blog/2008/12/why/' rel='bookmark' title='Permanent Link: Why?'>Why?</a></li>
<li><a href='http://www.sharms.org/blog/2009/02/python-commands-module/' rel='bookmark' title='Permanent Link: Python Commands Module'>Python Commands Module</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I have a camera which has a motor attached which I can rotate using a serial cable.  I figured it would be fun to have this camera analyze the webcam shots and turn in any direction there was motion.  I pulled out python and pygame, and created a prototype.  Unfortunately, I can&#8217;t make python go very fast.  I made two test cases, 1 in C and 1 in python, to figure out if it would be worthwhile to rewrite it:</p>
<p>array-speed-test.c</p>
<pre class="brush: c">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;sys/time.h&gt;

void compare_arrays(int **screen1, int **screen2)
{
    int x;
    int y;
    int diff;
    int mult;

    for(x = 0; x &lt; 1920; x++) {
        for(y = 0; y &lt; 1080; y++)
        {
            mult = screen1[x][y] * screen2[x][y];
            diff = abs(screen1[x][y] - screen2[x][y]);
        }
    }
}

int main(int argc, char **argv)
{
    srand(time(NULL));
    printf(&quot;Generating arrays...\n&quot;);
    int **screen1;
    int **screen2;
    int i = 0;
    int j = 0;
    struct timeval now;
    struct timeval end;
    int usecs_passed;

    screen1 = malloc(1920 * sizeof(int *));
    screen2 = malloc(1920 * sizeof(int *));
    for(i = 0; i &lt; 1920; i++)
    {
        screen1[i] = malloc(1080 * sizeof(int));
        screen2[i] = malloc(1080 * sizeof(int));
    }

    for(i = 0; i &lt; 1920; i++)
    {
        for(j = 0; j &lt; 1080; j++)
        {
            screen1[i][j] = rand() % 255;
            screen2[i][j] = rand() % 255;
        }
    }

    printf(&quot;Comparing arrays...\n&quot;);

    gettimeofday(&amp;now, NULL);
    compare_arrays(screen1, screen2);
    gettimeofday(&amp;end, NULL);

    usecs_passed = end.tv_usec - now.tv_usec;

    printf(&quot;Time passed: %dms\n&quot;, (usecs_passed / 1000));
    for(i = 0; i &lt; 1920; i++)
    {
        free(screen1[i]);
        free(screen2[i]);
    }

    return 0;
}
</pre>
<p>And my python code:</p>
<p>array-speed-test.py</p>
<pre class="brush: python">
#!/usr/bin/python
import random
import time
from math import fabs

def generateArray():
    array_to_gen = [None] * 1920
    for i in range(0, 1920):
        array_to_gen[i] = [None] * 1080

    for x in range(0,1920):
        for y in range(0, 1080):
            array_to_gen[x][y] = random.randrange(0,255)

    return array_to_gen

def compareArrays(screen1, screen2):
    for x in range(0, 1920):
        for y in range(0, 1080):
            diff = fabs(screen1[x][y] - screen2[x][y])
            combo = screen1[x][y] * screen2[x][y]

if __name__ == &quot;__main__&quot;:
    print &quot;Generating arrays...&quot;
    screen1 = generateArray()
    screen2 = generateArray()

    print &quot;Created two screens.  Comparing...&quot;
    startTime = time.time()
    compareArrays(screen1, screen2)
    print &quot;Time taken: &quot; + str((time.time() - startTime) * 1000) + &quot;ms&quot;
</pre>
<p>So far, the C program runs in 25ms, while the python program consistently takes 1100ms.  Might have to ditch python for real time analysis, unless someone wants to point out how I am doing this completely wrong (I am assuming the comments will be use Numpy?)</p>


<p>Related posts:<ol><li><a href='http://www.sharms.org/blog/2009/06/python-threads/' rel='bookmark' title='Permanent Link: Python threads'>Python threads</a></li>
<li><a href='http://www.sharms.org/blog/2008/12/why/' rel='bookmark' title='Permanent Link: Why?'>Why?</a></li>
<li><a href='http://www.sharms.org/blog/2009/02/python-commands-module/' rel='bookmark' title='Permanent Link: Python Commands Module'>Python Commands Module</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sharms.org/blog/2009/11/python-and-real-time-graphical-analysis/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

