Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Review my average rating algorithm

General discussions related to php

Moderators: macek, egami, gesf

Review my average rating algorithm

Postby maringtr » Fri Jul 06, 2012 1:54 pm

Hello everyone,

My name is Marin Bezhanov and I recently developed an algorithm that calculates average ratings faster than SELECT AVG() or any other PHP solutions based on the standard arithmetic mean equation. At this point, I'm looking for peer reviews and trying to receive as much feedback on it as I can. I'm also trying to popularize the algorithm.

The algorithm has been documented in detail here: Calculating Average Rating the Right Way using PHP and MySQL

Web-based benchmark running on my server using a 1 000 000 row data set can be ran here: Online Benchmark

Alternatively, you can download the benchmark script source and run it on your own server to compare the results. The files are available here: Benchmark Source Code

I would really appreciate your comments.

Thanks for your time!

Best regards,
Marin
maringtr
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Jul 06, 2012 1:46 pm

Re: Review my average rating algorithm

Postby johnj » Fri Jul 06, 2012 10:26 pm

The algorithm is calculating the average ONLY when a vote is cast. So, we cannot say that it is faster than sql AVG().

The reason why this cannot be compared with AVG() is there in your writeup...where it says..."...instead the rating can be displayed by just retrieving that single row stored in the 'average_ratings' table...".
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Review my average rating algorithm

Postby maringtr » Sat Jul 07, 2012 12:08 am

But of course it is still faster than SQL AVG()!

Even, if you only need to display the rating, and not recalculate when a new vote is cast, with my algorithm you just pull a single row from the 'average_ratings' table, which stores the current average rating and the total number of votes that it's based on, like this:

Code: Select all
SELECT average_rating,total_votes FROM average_ratings WHERE article_id = #### (1 row)


vs. AVG(), COUNT()

Code: Select all
SELECT AVG(grade), COUNT(grade) FROM votes WHERE article_id = ####


Here's a benchmark, so you can see for yourself: Display Average Rating Benchmark

Here's source code: Updated Benchmark Source Code

EDIT: Using a separate table to store the average rating and total votes count is still part of how this algorithm works. If it stores new ratings faster, re-calculates new ratings faster and displays new ratings faster, we can safely say it IS faster than MySQL AVG()
maringtr
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Jul 06, 2012 1:46 pm

Re: Review my average rating algorithm

Postby johnj » Sat Jul 07, 2012 1:20 am

I am not disputing your attempt and hard work.

But, What you have attempted is already used in many places, like:
(1) In http, http://static.usenix.org/event/osdi02/t ... ode19.html

(2). In Google search

I think even in dns. etc etc

Not only in voting, this idea is used in many places.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Review my average rating algorithm

Postby maringtr » Mon Jul 09, 2012 3:03 am

Thanks for pointing these materials out. I'll get myself acquainted with them. I've never heard the term "HTTP Prefetching", so it's always nice to learn things like this. That's exactly why I posted about my algorithm on forums, so more experienced PHP programmers, like you, can review and post their opinion. Thanks for your time, I really appreciate it!
maringtr
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Jul 06, 2012 1:46 pm


Return to PHP General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron