473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

I need help speeding up an app that reads football scores and generates rankings

About 10 years ago, I wrote a C app that would read scores from
football games and calculate rankings based on the outcome of the
games. In fact, I still use this app. You can view my rankings at
http://members.cox.net/jocknerd/football.

A couple of years ago, I got interested in Python and decided to
rewrite my app in Python. I got it to work but its painfully slow
compared to the C app. I have a file containing scores of over 1500
high school football games for last season. With my Python app, it
takes about 3 minutes to process the rankings. With my C app, it
processes the rankings in less than 15 seconds.

The biggest difference in my two apps is the C app uses linked lists.
I feel my Python app is doing too many lookups which is causing the
bottleneck.

I'd love some feedback regarding how I can improve the app. I'd like
to drop the C app eventually. Its really ugly. My goal is to
eventually get the data stored in PostgreSQL and then have a Django
powered site to process and display my rankings.

You can download the source code from http://members.cox.net/jocknerd/downloads/fbratings.py
and the data file from http://members.cox.net/jocknerd/downloads/vhsf2006.txt

Thanks!

May 2 '07 #1
4 1608
In <11**********************@h2g2000hsg.googlegroups. com>, jocknerd wrote:
The biggest difference in my two apps is the C app uses linked lists.
I feel my Python app is doing too many lookups which is causing the
bottleneck.
Then replace those linear searches you wrote in Python with a dictionary.

Ciao,
Marc 'BlackJack' Rintsch
May 2 '07 #2

"jocknerd" <je*******@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
| About 10 years ago, I wrote a C app that would read scores from
| football games and calculate rankings based on the outcome of the
| games. In fact, I still use this app. You can view my rankings at
| http://members.cox.net/jocknerd/football.
|
| A couple of years ago, I got interested in Python and decided to
| rewrite my app in Python. I got it to work but its painfully slow
| compared to the C app. I have a file containing scores of over 1500
| high school football games for last season. With my Python app, it
| takes about 3 minutes to process the rankings. With my C app, it
| processes the rankings in less than 15 seconds.

A ratio of 12 to 1 is not bad. However....

| The biggest difference in my two apps is the C app uses linked lists.
| I feel my Python app is doing too many lookups which is causing the
| bottleneck.

You have to do as many lookups as you have to do, but looking up teams by
name in a linear scan of a list is about the slowest way possible. Replace
'teamlist' with a dict 'teams' keyed by team name. Replace
'lookupTeam(team)' by 'if team not in teams: addTeam(team)' and delete the
lookupTeam function. Similarly 'lookupTeamRate(team)' becomes
'teams[team]['grate'] (and delete function). And
'updateTeamRate(team,rate)' becomes teams[team]['rate'] = rate' (and delete
function. And similarly for updateTeamRating and anything else using
teamlist. In many places, multiple lookups in teams could be eliminated.
For instance, 'team1 = teams[g['team1']]. Then use 'team1' to manipulate
its rating and other attributes.

| You can download the source code from
http://members.cox.net/jocknerd/downloads/fbratings.py
| and the data file from
http://members.cox.net/jocknerd/downloads/vhsf2006.txt

Minor point. Multiple functions do 'localvar = <expression>; return
localvar'. The simpler 'return <expression>' will be slightly faster.
Your comments and function name eliminate any documentary need for the
otherwise useless local var.

Function calls are relatively slow in Python. So calling
def totalPtsGame (score1, score2): return score1 + score2
is slower than simply adding the scores 'in place'.

Terry Jan Reedy
You can also, people say, use the profiler to find where time is going.

May 2 '07 #3
On May 2, 4:00 pm, jocknerd <jeff.s...@gmail.comwrote:
About 10 years ago, I wrote a C app that would read scores from
football games and calculate rankings based on the outcome of the
games. In fact, I still use this app. You can view my rankings athttp://members.cox.net/jocknerd/football.

A couple of years ago, I got interested in Python and decided to
rewrite my app in Python. I got it to work but its painfully slow
compared to the C app. I have a file containing scores of over 1500
high school football games for last season. With my Python app, it
takes about 3 minutes to process the rankings. With my C app, it
processes the rankings in less than 15 seconds.

The biggest difference in my two apps is the C app uses linked lists.
I feel my Python app is doing too many lookups which is causing the
bottleneck.

I'd love some feedback regarding how I can improve the app. I'd like
to drop the C app eventually. Its really ugly. My goal is to
eventually get the data stored in PostgreSQL and then have a Django
powered site to process and display my rankings.

You can download the source code fromhttp://members.cox.net/jocknerd/downloads/fbratings.py
and the data file fromhttp://members.cox.net/jocknerd/downloads/vhsf2006.txt

Thanks!
A simple improvement is to change your list of teams('teamlist') to a
dictionary of teams (call it say 'teamdict') mapping team names to
teams.

You have lots of
#Some code
for row in teamlist:
if teamname == row['name']:
#Do something with row

These can all be replaced with:
#Some code
row = teamdict[teamname]
#Do something with row

(Although I wouldn't call it 'row' but rather 'team')

That may speed up your code significantly.

Moreover you can make the main loop (in calcTeamRatings) faster by
avoiding looking up a team each time you need some info on it.

Finally I would change your schedule list to a list of tuples rather
than a list of dictionaries: each game in the schedule would be a
tuple (team1, team2, ratio) and wouldn't include the actual team
scores as you don't seem to use them in your calcTeamRatings function
(that means moving the ratio calculation into the loop that creates
the schedule)

Disclaimer: I only looked at your code superficially and I don't claim
to understand it !

HTH

--
Arnaud

May 2 '07 #4
En Wed, 02 May 2007 12:16:56 -0300, Marc 'BlackJack' Rintsch
<bj****@gmx.netescribió:
In <11**********************@h2g2000hsg.googlegroups. com>, jocknerd
wrote:
>The biggest difference in my two apps is the C app uses linked lists.
I feel my Python app is doing too many lookups which is causing the
bottleneck.

Then replace those linear searches you wrote in Python with a dictionary.
As an example: using a Team object instead of a dictionary, and using
teamlist (not a good name now) as a dictionary of Team objects indexed by
name:

def lookupTeam (teamname):
team = teamlist.get(teamname)
if team is None:
teamlist[teamname] = team = Team(teamname)
return team

def updateTeamStats (tname1, score1, tname2, score2):
team1 = lookupTeam (tname1)
team2 = lookupTeam (tname2)

team1.pf += score1
team1.pa += score2
if (score1 score2):
team1.won += 1
elif (score1 < score2):
team1.lost += 1
else:
team1.tied += 1

team2.pf += score2
team2.pa += score1
if (score1 < score2):
team2.won += 1
elif (score1 score2):
team2.lost += 1
else:
team2.tied += 1

Then you should realize that those last two blocks are too similar, and
you can make a function of it. And then you realize that in fact they act
on a Team object, so you should make a Team method...

--
Gabriel Genellina
May 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
6
by: Steven D'Aprano | last post by:
I've been working with the Borg design pattern from here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 and I'm having problems subclassing it. I'm a newbie, so I've probably...
3
by: Irene | last post by:
Hi all, I have set up a simple VB program (and later on an ASP interface) to manage an Athletics database. I'm using Access 2000. To simplify, I have the Athlets, the Competitions and the...
8
by: d24706 | last post by:
Hello, I hope someone can help me. I had to do the following assignment( i have most of it done just cant finish it off, question and source code below). Question? Write a Java program that...
16
by: Man-wai Chang ToDie (33.6k) | last post by:
Is it possible to build a Visual C++ GUI program without using MFC, using VC++ 2008 Express Edition? Forget about the time and speed issue. -- @~@ Might, Courage, Vision, SINCERITY. / v \ ...
2
by: shinerankin | last post by:
Hi Guys & Gals: I have a project with using flash that is updated with an xml file. The developer went belly up and is no longer assisting with anything. I need to write a simple html or php form...
3
by: sofh | last post by:
Hi, Can anyone guide me how to fetch the scores from http://www.cricinfo.com/rsadomestic-09/engine/current/match/423195.html to database using c#.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.