Written on Wednesday, April 9th, 2008 by Jeremy Steele
One of the most common PHP questions I receive is what is the best way to store data: indexed arrays, associative arrays, or classes (most likely with overloading). To answer this question I whipped up a quick little benchmark, which will return the time needed to A) store data via each type, and B) copy data for each type. This is not scientifically accurate by any stretch of the imagination - but I think it gives a pretty good idea which type is the quickest.
The Test
The script will run each loop 10,000 times by default, and this can be set via the $run_amt variable. The test will be run 20 times (set via $num_tests) and the results are added together, averaged, then displayed. You can view the output for each test by setting $verbose to true.
Here’s the script I used.
The Results
The results are in seconds:
Total Time:
Numerical Array: 0.2946305
Associative Array: 0.392692
Class: 0.364164
Averages:
Numerical Array: 0.014731525
Associative Array: 0.0196346
Class: 0.0182082
Conclusion
I tried running it on both my local server and the server that runs this site - and the differences between each data type were pretty much the same. Indexed arrays are the fastest, surprisingly classes (using overloading) were the second fastest, and associative arrays are in last place.
This isn’t very scientific, nor does it measure how much memory each type uses or how much CPU us required - but I think it gives a good idea which type is better.
I think if you’re going to be storing a little bit of data, it doesn’t matter which type you use. The differences were barely there when I ran the test with 100 loops instead of 10,000. However, when storing large amounts of data indexed arrays ($i[1], $i[50], etc) are probably the best choice.
If you can come up with a better test, go for it. I wrote this thing in about 10 minutes at midnight - so I wouldn’t be surprised if there’s a better way to do the benchmark. I have a feeling the results will be fairly similar though.
Written on Wednesday, April 2nd, 2008 by Jeremy Steele
One neat little feature of WordPress 2.5 is built in support for Gravatars via the get_avatar function. Simply use it in your theme’s comment loop in the comment.php file, and boom, you’ll have avatars. Nusuni Dot Com already supported Gravatars thanks to the plugin - but today I switched it over to the new function.
One little warning I’d like to pass along is make sure you use echo! While implementing it I did this:
<?php get_avatar($comment,64); ?>
There’s one big problem with that code - it doesn’t work! The solution to this problem was a simple programmer-error, I forgot echo! It took me almost thirty friggin minutes to figure that one out!
So yeah, make sure you use echo.
<?php echo get_avatar($comment,64); ?>
If this isn’t proof I’m an idiot, I don’t know what is.
Written on Wednesday, April 2nd, 2008 by Jeremy Steele
During today’s little RSS reading ritual I realized something many blogs have in common - they don’t notify their commentators if their comment is thrown in the moderation queue or spam bucket. Now that isn’t very user-friendly, is it?
I really don’t get it at all. It only takes 3 lines of code to display a simple message if a comment needs to be moderated. Simply add this code in your comment.php file for your theme:
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?>
Now that wasn’t hard, was it? The best place to add it is right next to the commentator’s name, so it appears as “Jeremy Says: Your comment is awaiting moderation.” Just do a search in the comment.php file for “comment_author_link()” and add it right after that.
Written on Saturday, March 22nd, 2008 by Jeremy Steele
After a fairly hectic week I finally got around to installing a test version of WordPress 2.5. As far as I can tell with WordPress 2.5 you can change the feed url for incoming links to point to Technorati, and it’ll work perfectly without my plugin:
http://feeds.technorati.com/cosmos/rss/?url=YOUR_BLOG_URL_HERE
A few days ago I asked Mohsin about trying it out and he said it reported some sort of error about the feed not existing, I’m not sure if there was a glitch or something, because it seems to be working fine now.
Just my feelings on 2.5 really quickly - RC1 seems to be running much slower than 2.3.3 on my test server, and while the new admin page is cool in that it is customizable - I really dislike the new look. It’s just fugly.
Update: While testing a plugin I’m working on for someone, I tried fiddling with some of my settings for the 2.5 install and found out one of my changes to the code was causing a little slowdown in the admin panel. It seems to be running at full speed now. Still not as fast as 2.3.3, but it’s close enough.
Written on Tuesday, December 4th, 2007 by Jeremy Steele
I just got an e-mail from someone who says their Dashboard is not showing their Technorati Links. This can happen for a few reasons:
- Bad data from Technorati
- Bad request (unlikely, unless the data on the options page is entered wrong)
- WordPress error
- Magical fairies are being mean (curse those fairies!)
Chances are it is just a bad connection to Technorati. Version 1.0 of the plugin handles the connection itself (although it uses the built in RSS functionality of WordPress - so really WordPress still handles it), and version 1.1 simply uses a filter to give WordPress the Technorati URL. The plugin does very little by itself - so odds are the error is with WordPress or Technorati.
There are still some basic things to try before getting mad at me (grr Jeremy… grr… fix this bug!) -
- Make sure the options page is filled out correctly
- Wait a few hours and see if it is a Technorati server glitch
- Disable and reenable the plugin
- Disable, redownload, reupload (delete it off the server first), then reenable it
- Bitch at me
If you still get an empty links list just drop me an e-mail with your blog URL (or the URL entered in the options page), and I will try to figure it out. Chances are I won’t be able to do much though - like I said before, the plugin itself does very little.
If you feel adventurous - you could always try manually doing the same thing the plugin does. Open up the /wp-admin/index-extra.php file and change the URL after the dashboard_incoming_links_feed filter info to:
'http://feeds.technorati.com/cosmos/rss/?url=' . trailingslashit("YOUR BLOG URL HERE")
and change the URL after dashboard_incoming_links_link to:
'http://technorati.com/blogs/' . trailingslashit("YOUR BLOG URL HERE")
And see if that works. Oh, and make sure your blog URL is surrounded by quotes, as in the examples above. The two URLs you need to replace are right on the top of the file and are hard to miss.