Just a quick time comparison of PHP’s preg_replace vs. str_replace
Here's a short and succinct comparison of running time to replace multiple characters in a given string. I ran the test 20 times each, so the numbers you will find are average running times:
$start = microtime(); $str = "23ilrj23oirj23iorj o23irj23klfj23lkjr4ocimior 4r ioj234roij234r io34jrio4jrio34r jio4jr o34jr oi4jr io34 r"; $new_string = preg_replace('/[\w2]/',',',$str); $end = microtime(); echo($end - $start); echo "\n"; $start = microtime(); $str = "23ilrj23oirj23iorj o23irj23klfj23lkjr4ocimior 4r ioj234roij234r io34jrio4jrio34r jio4jr o34jr oi4jr io34 r"; $new_string = str_replace(array('2',' ',"\t"),',',$str); $end = microtime(); echo($end - $start);
The results:
regex: 0.000608
tr: 0.00024099999999999
Facebook Application: Hello Birthday
About a month ago it was my birthday. On that day I was receiving countless e-mails from Facebook notifying me of Happy Birthday messages when I realized many of the messages followed a particular pattern (i.e. "Happy Birthday(!| <name>,| <endearing term>). Don't get me wrong, I enjoy and appreciate all of my birthday wishes. But being an Engineer, my brain finds patterns and attempts to "code up solutions." However, I had not played with the Facebook API yet and had been waiting for a good reason to come around so I took the idea of a Happy Birthday messenger to work and put together a version one of my Hello Birthday application. The application follows a few goals: to be easy to understand, easy to use, and it should just work. With that, here are the criteria I came up with to take a crack at achieving these goals:
- Automatically message friends on their birthday
- Allow enabling and disabling of such said feature
- Use a generic message that more than 50% of users observed currently use when wishing a friend "Happy Birthday"
- Manage an exclusion list, containing the friends Hello Birthday should not automatically message
- Post onto the friends' wall as if it were the user
With plans to continue development, I have a more robust friend list module going in soon and the ability to set up custom messages for particular friends. Eventually, some form of gift giving may be in order. To bootstrap development I followed a few principles:
- Prototype over masterpiece
- Design impacts implementation, but implementation makes design possible
- Low budget: use open source and free resources
To prototype the project I chose to use Ruby on Rails and SVN. To bridge my rails application and Facebook, I added the Facebooker rails plugin to my application. Facebooker is a pretty neat extension to Rails that provides all kinds of helper methods and an extremely easy to use interface wrapping the Facebook API. It also provides nice view helpers for generating FBML (the Facebook Markup Language) and FBJS (Facebook JavaScript). Developing version one locally saved quite a bit of time by realizing change immediately as opposed to any form of deployment. I then set-up a Facebook application (in Sandbox mode) for development (i.e. hello_birthday_dev) that only I had access to. Facebook offers test developer accounts, so after registering 6 or 7 of these to use with tests I was able to enumerate all of the states that Hello Birthday would need to recognize and transition.
When it came time to deploy to production I needed a production server and Facebook application. I registered the Facebook application (again, in Sandbox mode) for production (i.e. hell0_birthday) and then signed up for hosting with Heroku.com. Heroku is a Ruby (mostly Rails) cloud computing platform as a service. Basically, you get one free "cpu worker" and as you need more they sell scalable features. This, however, is perfect for what I need: a Rails host that allows cron jobs and makes deployment super fast & easy. Sure enough, Heroku makes deploying an application as easy as:
- git add .
- git commit -a
- git push heroku master
A slug of my app is created and deployed to my birthday.heroku.com production server. And to make sure I can monitor errors, I installed the hoptoad error monitoring service. Hoptoad app provides me with web-based access to error reporting and resolution management (organizes errors based on environment; test, production, and or development).
The training wheels were taken off: Sandbox mode disabled on Hello Birthday. I submitted my application to the Facebook directory a week or so ago and it's been smooth sailing since. I currently am servicing a little over 40 people. No complaints have been submitted thus far.
In summary, this is my first Facebook application and I have tried to utilize a lot of the latest technology in the process to illustrate how quickly a scalable solution to an easy problem can be solved. With Heroku & fairly efficient code, I do not foresee a problem handling a potentially high growth rate. I will try to bring back updates to this article if anything goes terribly wrong.
You can check out the Facebook application at http://apps.facebook.com/hello_birthday
AnswerWise — The power of humans
Over the last few weeks I have run into numerous spiders and being the inquisitive type of person I am I always want to know what I am looking at. Thanks to my senior capstone project at the University of California at Santa Barbara, I finally have an answer to pretty much any question I could formulate... And the best part about AnswerWise is that it supports images. Win win. Here is the latest question asked and it's answer. (AnswerWise had the answer in under 10 minutes)
What kind of spider is this and is it poisonous?
Answer: This is a Phidippus johnsoni (Red-backed Jumping Spider) its bite results in swelling and pain at the bite site but its not poisonous. But of course if you are allergic it will be worse.
Pretty cool huh? I think so. A little bit about our senior project statistics: Over 800 questions have been asked with an average 15 minute response time and 80% or more of the asked questions include images.
Ruby Regular Expressions – Security Risk
This post is a half reminder to elaborate when I have free time... But in short, there is nothing wrong with Ruby regular expressions, except that they behave differently than one might expect (in general and if coming from Perl RegEx).
Here is the dealy, from the Programming Ruby book by Dave Thomas:
The patterns ^ and $ match the beginning and end of a line, respectively. These are often
used to anchor a pattern match: for example, /^option/ matches the word option only if it
appears at the start of a line. The sequence \A matches the beginning of a string, and \z and
\Z match the end of a string.
All sounds good right? Well, it turns out that Ruby will execute code within a regular expression if you can pass multi-line input to the parser. For example... Given
class EmailAttachment < ActiveRecord::Base validates_format_of :attachment, :with => /^[\w\.\-\+]+$/ end
You can easily pass in
attachment.txt%0A<script>alert('open_sesame')</script>which is converted (as %0A is a URL encoded new line), by ROR, into
"attachment.txt\n<script>alert('open_sesame')</script>"You can think about the implications of this, feel free... I have been able to have some fun with my own personal site and getting arbitrary JavaScript and (worse) shell commands to execute. Also, I believe this may cause a larger security whole within routes for Rails (at least 2.1.0). I'll investigate this more later, as the beginning of this post says.
An unexplained data loss & what’s up
At some point over the past week the hosting company I use had a 'hard drive issue' and unfortunately had to revert all hosting accounts to data from around December 2nd, 2008... Yes, the last back up they had was over 2 months old. Not only is it unsettling that they had such _old_ back ups of their clients software, but I have checked my e-mail history and see no communication from them letting me know that I had lost all of the last two months of progress and data. Upon logging into my client control panel I noticed a 6 month credit for hosting -- does this somehow make it okay for 2 months of work flushed away? No. In fact, I'd rather have those two months of work back. Luckily, I back up the important things every now and then. Still, the urking continues as I see in my e-mail inbox news from the hosting company to do data migrations to new 'more powerful' servers. Let me ask this: Will the power of these servers keep the hard drives from having 'issues'....... Issues are bad m'kay.
If you noticed down time or old content it has now been fixed and I apologize for the potential 'content not found' messages that awaited viewers access attempts.
As of lately, however, I have been doing something pretty neat: teaching a Ruby on Rails workshop on my college campus to students. I wanted to try introducing web framework technology + newly popularized programming languages so naturally Ruby on Rails was a perfect fit. I've now constructed 2 workshops, each lasting roughly 6 hours, and have had extremely high interest and attention spans from students. In the coming week I will be having at least 3 workshops and they take place on weekends. I'll be writing up information about that soon, so if you are interested in learning Ruby on Rails and are local Santa Barbara, CA I encourage you to attend. If you don't even know what Santa Barbara is I still encourage you to learn more by checking out the presentation I will be posting in the following weeks. (Expressing interest in this presentation will speed up how quickly I post it...)
High Speed Cable Companies monitoring bandwidth?
It has come to my attention, through Slashdot and the many online sources, those bandwidth providers are now looking at the amount of bandwidth used by a customer and then shutting their internet off when a certain invisible limit is reached.
I personally felt this was inevitable and that companies have been doing this for quite some time. It makes sense that companies are starting to announce their hidden agenda’s at this time, because internet bandwidth consumption has risen at such great rates due to video on demand, sites like YouTube, and the fact that the internet is evolving into one large application.
We will see where this goes. Bandwidth is measured at a small level in terms of MB (megabytes) and most commonly measured as GB (gigabytes, which are 1,000 MB). If the average consumer uses 20GB of bandwidth monthly, can you imagine what it will cost for the high tech user who uses 100GB/mo in bandwidth?
Related articles/sources:
- Game Daily - [article]
