You are currently browsing the daily archive for August 3rd, 2008.

Which to use for an array? The answer is: foreach. Not only would you be able to get the keys and values, but it’s faster. If you don’t believe, try this (please excuse the extravagant amount of text):

<?php

/*
Tested, one at a time.
*/

function loadTime() {
$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
return "<br /><br />Processing time: ".(microtime() - $time1)." seconds.<br /><br />";
}

//3266 bytes, not including the line breaks.
$string = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus nibh lectus, faucibus in, vehicula quis, gravida sed, risus. Fusce et neque venenatis felis faucibus placerat. Donec tincidunt, nisl rhoncus consequat tincidunt, nisi neque pharetra elit, et posuere sem nisi at magna. Nulla facilisi. Integer pretium elementum velit. Ut lectus mauris, lobortis vitae, blandit in, interdum pretium, tortor. Donec augue mauris, mollis hendrerit, laoreet sit amet, condimentum in, nulla. Suspendisse facilisis pharetra urna. Maecenas quis diam. Sed facilisis accumsan sapien. Ut feugiat metus et quam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer egestas tortor ut sem. Sed venenatis pede ac orci sagittis vehicula. Suspendisse sed arcu eget magna dignissim adipiscing. Maecenas dapibus, augue id vehicula fringilla, nulla pede condimentum risus, sit amet blandit lectus felis nec lorem. Quisque mi urna, cursus at, ultricies sed, adipiscing a, libero.\n\nDuis magna ipsum, placerat eget, aliquam eu, condimentum et, felis. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec nisi. Vivamus ac odio at nunc elementum lobortis. Aliquam erat volutpat. Nunc pellentesque adipiscing metus. Pellentesque nec tellus sit amet ligula gravida fringilla. Nunc consequat eleifend purus. Pellentesque nec neque. Ut vestibulum dui eget tellus. In laoreet hendrerit mauris.\n\nQuisque condimentum, quam sit amet vehicula cursus, nulla pede lobortis sem, nec luctus felis justo nec orci. Phasellus diam est, adipiscing a, ornare vitae, mollis in, nisl. Fusce purus quam, tincidunt ac, tristique feugiat, dapibus in, tortor. Duis ultrices. Pellentesque sed sapien non metus egestas porttitor. Ut nec metus. Aenean rutrum. Maecenas nec est. Cras sed enim. In sed leo at ligula vehicula interdum. Fusce iaculis dui quis nulla. Sed aliquam erat. Nullam in neque sollicitudin libero dictum porta. Ut tempor risus sed nunc.\n\nNam sit amet tellus. Donec semper, turpis in luctus aliquam, pede nulla vulputate diam, vitae eleifend turpis lectus vitae quam. Vivamus pellentesque. Morbi malesuada purus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nunc tincidunt nulla at sapien. Aenean non dui ac lectus rutrum pulvinar. Nam pulvinar quam vitae massa. Donec laoreet. Nunc eleifend ligula in metus. Etiam tincidunt. Morbi posuere enim et ipsum. Nulla nisi. Aliquam vel tortor. Donec pulvinar eros id nisl. Pellentesque in tellus. Sed orci lacus, tincidunt at, consectetuer ac, rhoncus eget, mi.\n\nInteger semper malesuada sem. Fusce in enim. Pellentesque varius semper libero. Suspendisse eu augue placerat arcu tincidunt pellentesque. Morbi tristique velit eget elit. Praesent fringilla viverra nunc. Duis accumsan nibh vitae nisi. Donec cursus vehicula dui. Vivamus massa est, malesuada ac, porttitor a, ultrices eget, mi. Integer gravida aliquet lectus. Phasellus laoreet luctus leo. Duis eget pede. Quisque ligula massa, dictum id, laoreet sit amet, ornare vel, mauris. Morbi sapien. Donec placerat malesuada quam. Curabitur iaculis. Etiam pellentesque arcu feugiat tellus. Praesent pulvinar euismod ligula. Cras ante sem, blandit dictum, eleifend ut, venenatis non, metus. ";
$explosion = explode(" ", $string);

print loadTime();
foreach($explosion as $k => $v)
{
print $k.":".$v."<br />";
}

print loadTime();
for($i = 0, $cnt = count($explosion); $i < $cnt; $i++)
{
print $explosion[$i]."<br />";
}

print loadTime();

?>

Results:

Processing time: 0.997527 seconds. (explode)
Processing time: 0.003688 seconds. (foreach)
Processing time: 0.004356 seconds. (for)

Using PHP5. Might depend on your server and PHP version.

Sakura Matou is probably my favorite girls of all the ones that I know. She’s on par with Yuki Nagato-chan.

Download-able version.

The import command in Python is one of my favorites (not joking). import allows the programmer to link (load) files and inherit all of the contents of that file (you can use whatever is in that file in another file). Here is the basic syntax:

import [module]

[module] is also called a file, but they’re referred to as modules. I’ll explain modules a little later. A little note: Python will execute whatever is in the file if it can. Meaning, you cannot store a file in a variable this way. It’d be like storing a file in a variable by using #include (C++) or require (PHP). Also, it must be a Python file (.py) and you do not include the extension (why is also explained). Finally, import loads the modules that the current file is in; for example, if you import a module and the file is the directory C:\Testing\Build, then Python will load (and execute) C:\Testing\Build\[module_name].

Ok, so say you don’t want to load the entire file. What is there to do? You can load a class from the file, like so:

from [module] import [class]

This means, from the module that is [module] only take the class that is [class]. Meaning, if you do this,

from foo import bar

Python will load bar from foo and only bar from foo. It won’t load anything else. You can use multiple classes as well,

from foo import bar, stool

Python will load only bar and stool from the module that is foo. Likewise, you can load multiple modules as well,

import foo, settings

However, you cannot do this,

from foo, settings import bar, UI

This will generate a syntax error, the most common error. Now, onto modules. A module is anything with Python code in it – that includes folders. Python supports folders acting as modules. How is this done? Like this,

import [folder_name]

The only difference is that there needs to be a __init__.py file in [folder_name]. Otherwise, Python will think the module doesn’t exist. Why would you want to do this? Mainly for organization. You can do this,

import [folder_name].[module]

Python will load [module] that’s in [folder_name]. Meaning, you can do this,

import source.settings

and Python will load the file source/settings.py. As long as there’s a source/__init__.py file, it’ll work. In fact, you can even do this,

from source import settings

and it’ll work. You cannot do this:

import source/settings

You’ll get a syntax error. You especially should not do this:

import source\settings

That’s just plain stupid and one of the worst syntax errors one can make.

I am not sure what the correct name is for this feature, but it does make function calling more convenient. Take this for an example:

<?php

function hold_the_line($soldier='Major Rawne',
$regiment_leader='Colonel-Commissar Gaunt',
$planet='Tanith') {
	print "Soldier: ".$soldier;
	print "Regiment Leader: ".$regiment_leader;
	print " from ".$planet;
}

hold_the_line($planet='Vervunhive');

?>

This is what I am talking about. PHP will completely ignore, deliberately or other, the $planet, thus outputting,

Soldier: Vervunhive, Regiment Leader: Colonel-Commissar Gaunt from Tanith

This makes default arguments all the more difficult. If you do this in Python,

def hold_the_line(soldier='Major Rawne',
regiment_leader='Colonel-Commissar Gaunt',
planet='Tanith'):
	print "Soldier: " + soldier
	print "Regiment Leader: " + regiment_leader
	print " from " + planet

hold_the_line(planet='Vervunhive')

you would get this,

Soldier: Major Rawne
Regiment Leader: Colonel-Commissar Gaunt
 from Vervunhive

Cool, eh? PHP needs this. Not sure if it’ll appear in PHP6.