Category: idea

Counting occurrence of a word in a String :: Benchmarking of PHP functions

Today I was just thinking what are the possible ways to count the occurrence of a specific word inside a string. I found some possible ways finally and I just benchmarked them. Wanna see the result?? – for sure you will find it interesting too.

<?
function microtime_float()
{
   list(
$usec$sec) = explode(” “microtime());

   return ((float)$usec + (float)$sec);
}

$str “I have three PHP books, first one is ‘PHP Tastes Good’,
 next is ‘PHP in your breakfast’ and the last one is ‘PHP Nightmare'”
;

$start microtime_float();
for (
$i=0$i<10000$i++)
{

    $cnt count(split(“PHP”,$str))-1;
}
$end microtime_float();

echo “Count by Split+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();

for ($i=0$i<10000$i++)
{
    
preg_match_all(“/php/i”,$str,$matches);

    $cnt count($matches[0]);

}
$end microtime_float();
echo 
“Count by Preg_Match+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();

for (
$i=0$i<10000$i++)
{

    str_replace(“PHP”,“PP”,$str,$cnt);
    
//echo $cnt;
}
$end microtime_float();

echo “Count by str_replace took : “.($end$start).” Seconds\n”;

$start microtime_float();

for ($i=0$i<10000$i++)
{
    
str_ireplace(“PHP”,“PP”,$str,$cnt);

    //echo $cnt;
}
$end microtime_float();
echo 
“Count By str_ireplace took : “.($end$start).” Seconds\n”;

$start microtime_float();

for (
$i=0$i<10000$i++)
{

    $cnt count(explode(“PHP”,$str))-1;
    
//echo $cnt;
}
$end microtime_float();

echo “Count By Explode+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();

for (
$i=0$i<10000$i++)
{
    
$word_count = (array_count_values(str_word_count(strtolower($str),1)));

    ksort($word_count);

    
$cnt $word_count[‘php’];
}
$end microtime_float();
echo 
“Count By Array Functions took : “.($end$start).” Seconds\n”;

$start microtime_float();
for (
$i=0$i<10000$i++)

{
    $cnt count(preg_split(“/PHP/i”,$str))-1;
}
$end microtime_float();

echo “Count By preg_split+Count took : “.($end$start).” Seconds\n”;

$start microtime_float();
for (
$i=0$i<10000$i++)
{

    $cnt substr_count($str“PHP”);
}
$end microtime_float();
echo 
“Count By substr_count took : “.($end$start).” Seconds\n”;

?>

And the result is

First Run
Count by Split+Count took : 0.44112181663513 Seconds
Count by Preg_Match+Count took : 0.46423101425171 Seconds
Count by str_replace took : 0.23512482643127 Seconds
Count By str_ireplace took : 0.39766597747803 Seconds
Count By Explode+Count took : 0.25045800209045 Seconds
Count By Array Functions took : 1.1077101230621 Seconds
Count By preg_split+Count took : 0.30741000175476 Seconds
Count By substr_count took : 0.21060705184937 Seconds

Second Run

Count by Split+Count took : 0.68125295639038 Seconds
Count by Preg_Match+Count took : 0.60020899772644 Seconds
Count by str_replace took : 0.2877471446991 Seconds
Count By str_ireplace took : 0.47500586509705 Seconds
Count By Explode+Count took : 0.31055402755737 Seconds
Count By Array Functions took : 1.3551599979401 Seconds
Count By preg_split+Count took : 0.40205383300781 Seconds
Count By substr_count took : 0.24432802200317 Seconds

Third Run
Count by Split+Count took : 0.50134515762329 Seconds
Count by Preg_Match+Count took : 0.53588891029358 Seconds
Count by str_replace took : 0.25469994544983 Seconds
Count By str_ireplace took : 0.34696006774902 Seconds
Count By Explode+Count took : 0.23176002502441 Seconds
Count By Array Functions took : 1.0504789352417 Seconds
Count By preg_split+Count took : 0.28686618804932 Seconds
Count By substr_count took : 0.20796585083008 Seconds

Fourth Run
Count by Split+Count took : 0.4736020565033 Seconds
Count by Preg_Match+Count took : 0.48813104629517 Seconds
Count by str_replace took : 0.29280996322632 Seconds
Count By str_ireplace took : 0.51396799087524 Seconds
Count By Explode+Count took : 0.34470105171204 Seconds
Count By Array Functions took : 1.4177949428558 Seconds
Count By preg_split+Count took : 0.36489319801331 Seconds
Count By substr_count took : 0.27841401100159 Seconds

If you are interested to know the machine configuration, these tests ran on a Celeron 1.6GHz processor based laptop with 768 MB of RAM. And I am using PHP 5.1.1

CookieJar in CURL – It Sucks

I was working with Linked in authentication management these days for one of my project where I have to loginto linked in using user’s credentials and fetch personal information and then display it in different form. My code was working properly in local machine, 3 different LAMP servers and one windows server. But finally when I deployed the code in production box, it fails. I quickly found that the cookiejar was not created for some permission problem. I tried to figure out what went wrong but I cant.

1. My script has the permission to create file in that directory on the fly

so there shouldn’t be any problem with this cookie jar and curl, but there was. So hours after hours i spend on it to find the reason and finally I decide to go without cookie jar. And If I don’t use cookie Jar, I have to manually parse the cookies sent to me after login and then set those cookies to my next request so that Linked in recognize me as a “coming back call after login”. I did that and my script worked pretty fine.

Fuck cookie jar in curl. Why the hell the developers didn’t provide a way to override to manage cookies??

If you are interested to know how did I solve it, let me explain a bit.

I set CURLSETOPT_HEADER to true so that I get back the header info
curl_setopt($ch, CURLOPT_HEADER, 1);

and then I parse that header info and extracted the cookies
$end = strpos($header, “Content-Type”);
$start = strpos($header, “Set-Cookie”);
$parts = split(“Set-Cookie: “,substr($header, $start, $end-$start));
$cookies = array();
foreach ($parts as $co)
{
$cd = split(“;”,$co);
if (!empty($cd[0]))
$cookies[] = $cd[0];
}

I will replace this section with RegEx

and finally I set those cookies to my next request using CURLSETOPT_COOKIE
curl_setopt($ch, CURLOPT_COOKIE, implode(“;”,$cookies));

Thats it!! It works pretty fine without cookie jar.

NOTE: I know that cookiejar is a very useful feature for curl users as it automates the cookie management. But I am saying “fuck cookiejar” because developers of curl didn’t provide any way to override cookie management process. If they give us way to use cookijar with any other options beside disk files, it would be beautiful. But in fact I am a big fan of this cookijar feature of curl, except the selfish automation.

Javascript For Living?? Oh hell yeah….

I just came across this article in Ajaxaian where Dion Almaer rant what you need to do when you code JS for living!!! Some says that pure JS is nothing, some says that just JS cant take you anywhere. I agree partially with Dion’s thought and wants to add something by myself.

Well, you can do pure JS for living as I am doing right now. Beside lots other tasks in pageflakes, I write huge amount of JS everyweek to develop flakes as a development engineer. Beside that I also maintain the community, study JS in the user submitted flakes and support them to write efficient JS. If you neglect JS as a language, you are doing a big mistake. JS is extremely powerful and you can do things beyond your imagination with it.

I want to add some tips beside dion’s suggestions. You also need to do the following if you really want to live by coding JS.

1. DOM Scripting is a Must, but not all, simple Node operations will do.
2. Interacting with CSS is also a must.
3. Manipulating JSON is more important than XML becoz its much more flexible and lightweight to transfer your data as JSON object.
4. Choose some popular libraries and stick to them. Personally I suggest Prototype (Its a must), jQuery, mooTools and Scriptaculous. I dont like Dojo becoz it seems too heavier to me. (I dont agree with Dino in this issue, I suggest DRY, Dont Repeat Yourself. Why do you have to reinvent the wheel?)
5. AJAX is also a must.
6. Being able to write JS in Object Notation format (he he he, its JSON)
7. keep yourself uptodate by visiting sites which inform you what’s actually going on in this sector.
8. Be aware of browser compatibility issues (Its a must)

And Finally

9. Love JS

I got a cool idea – Now developing this wordpress plugin

After getting the solution for sending message to MSN contact I got another cool idea. I can easily develop a small plugin for wordpress using which people will be able to subscribe to any blog with their MSN address. and When that blog will update, that plugin will automatically send new post URL to those subscribed MSN addresses.

Whoa!!

I have started developeing this cool plugin for wordpress.

Cool Messenger Libraries for Developers

I have been planning to develop a localized messenger service for Bangladeshi peoples for quite a long time. I studied a lot on this topic and many times attempted to kick start the development, but unfortunately I ran out of time every time. But what I found is a wide range of libraries available for developers to develop such a service without spending a great amount of time. Let me list some of them here for You

1. Wildfire IM Server with IM Gateway [Java]
2. sendMsg [PHP For MSN (The Best)]
3. MSNP [PHP for MSN]
4. Joscar [Java for AOL/ICQ]
5. jYMSG[Java For YM]
6. TjMSNLib [Java for MSN]
7. Java-Jml [Java for MSN]

Are you interested????

Bring some Ruby/Prototype flavour in your PHP array

You know that in ruby/prototype you can traverse thru each element of array like this Array.each(function(){/*function body*/}). It has also some methods like without(), inspect(), indexOf();last(),  first() and others…. so how about implementing these cool methods in your regular PHP array?? No problem, lets extend the ArrayObject and have some fun. Here is the class.

class ExtendedArrayObject extends ArrayObject {

        private $_array;
        public function __construct()
        {
                if (is_array(func_get_arg(0)))
                $this->_array = func_get_arg(0);
                else
                $this->_array = func_get_args();
                parent::__construct($this->_array);
        }

        public function each($callback)
        {
                $iterator = $this->getIterator();

                while($iterator->valid()) {
                        $callback($iterator->current());
                        $iterator->next();
                }

        }

        public function without()
        {
                $args = func_get_args();
                return array_values(array_diff($this->_array,$args));
        }

        public function first()
        {
                return $this->_array[0];
        }

        public function indexOf($value)
        {
                return array_search($value,$this->_array);
        }

        public function inspect()
        {
                echo “<pre>”.print_r($this->_array, true).”</pre>”;
        }

        public function last()
        {
                return $this->_array[count($this->_array)-1];
        }

        public function reverse($applyToSelf=false)
        {
                if (!$applyToSelf)
                return array_reverse($this->_array);
                else
                {
                        $_array = array_reverse($this->_array);
                        $this->_array = $_array;
                        parent::__construct($this->_array);
                        return $this->_array;
                }
        }

        public function shift()
        {
                $_element = array_shift($this->_array);
                parent::__construct($this->_array);
                return $_element;
        }

        public function pop()
        {
                $_element = array_pop($this->_array);
                parent::__construct($this->_array);
                return $_element;
        }

}

######################################

Now you can use it like this

$newArray = new ExtendedArrayObject(array(1,2,3,4,5,6));

or you can even construct it like this

$newArray = new ExtendedArrayObject(1,2,3,4,5,6);

then you can use these methods

function speak($value)
{
  echo $value;

}

$newArray->each(speak)
$newArray->without(2,3,4);
$newArray->inspect();
$newArray->indexOf(5);
$newArray->reverse();
$newArray->reverse(true); /*for changing array itself*/
$newArray->shift();
$newArray->pop();
$newArray->last();
$newArray->first();

So how is this ExtendedArrayObject, like it?

Regards
Hasin Hayder
http://hasin.wordpress.com

Qucik and Dirty – using 2 DB concurrently in MySQL

Day b4 yesterday I was stuck with a simple and frustrating problem. You know that using builtin mysql support in PHP you cannot use concurrently more than one database to work with. That was also my problem while working in such a project where I badly need to read data from one database, validate it against existing data in second database and then insert the data into the second database. So you got the project? Yes its a simple data migration project. But the problem is that I cannot import the old database tables into the new one and I have to work with them keeping seperate.

So you may ask how about reading all the data from first database, then store it in a temporary media and then again read them while inserting in second db. Well that was not possible because in first DB I had 10 tables and in I have to merge all those info in two table in second database. Also I have to validate some of those data against existing data in second DB.

There comes the idea. What I did is a quick-n-dirty solution of this problem. I connected to the first DB using MySQL API, and I connected to the 2nd DB using MySQLi API. That gives me live connection of two DB at the same time. So I read data from first DB using MySQL API and then inserted using MySQLi API.

For sure I could apply another solutions like temporary storage. But why bother when you can trick??? This is just a one time operation, no matter how you do it. All you need is a merged output, at minimum efforts. Ha Ha Ha.

WordPress4SQLite – alpha one

The project finally see some light. It comes to a prematured state where it is working smoothely, but with lack of some features. I have to go thru every piece of code where SQL statements are present and I make them SQLite compatible. The project is not finished, Category and Image upload is not working properly but I will complete them next week. I have been suddenly bz in another official project which I have to complete within next 5 days, so I may not be able to contribute before next week. But I am extremely confident that I can solve those problems.

You can download this prematured version of WordPress4SQLite from the following link and just after modifying the config file as usually, you can proceed.

Please note that error reporting has been turned off just to save you from some annoyances :D. If want to turn error reporting on, open wp-includes/wp-db.php and remove the error_reporting() function.

There is a sample config.php file present already. You can modify it to kickstart using WordPress4SQLite. Please use absolute path for database file.

Download From Here : WordPress4SQLite

You can see a running version of WordPress4SQLite here

A new project – WordPress SQLite

WordPress runs pretty fine in MySQL. But what if I want to run it in SQLite or in PostgreSQL? No way!!

Last night I started a new project and I am converting wordpress DB-Core as well as the db schema to SQLite. It will be a great project if successful.

I hope I can release it within 3-5 days.

Long Live WordPress.

Bangla Chat – What I am planning right now

I had a dream of a complete bangla chatting portal for a long long time. I worked irregularly from time to time but those never see the light, sad. However I ambeing serious day by day and just after completion of my current book in august, I will start working for a bangla chat system. Lets have a look of what could be done and whats not.

I can develop a script which works as a IRC client and people can post in bangla. Both a desktop solution and web based solution is feasible, but I emphasize for the web based one.

A localized ymsg(yahoo), msn(MSN0 or jabber(gTalk) protocol based messengers would be a fantastic (Awesome) thing. I collected resources for working and probably these would be the things I planned for my life. For yahoo messenger jYMSG would be the best resource. Also PlanetaMessenger would add some value for developers. for MSN TjMSNLib is a great piece of library. And for Jabber protocol I need to study a bit. GTalk provides information for developers too.

Third choice, last but not the least, is a total web based chat interface where I have to manage all the transactions in a local database. This could extend the development time but can bring ultimate flexibility for developers.

So what makes me disappointed? From my recent experience I am very much concerned about licensing and copyright issues. Hasan, one of my colleague from somewherein… java desk recently developed a mobile based yahoo messenger client. Everything wen fine but it was very embarrassing when comes the licensing issues. If you use yahoo messenger protocol or msn protocol for commercial purpose, you may have to pay a huge amount of money for licensing, specially for mobile based solutions.

But I am going for Both a localized version of popular messenger protocols as well as a flexible web base chatting portal. Lets see!!