Même en codant proprement et bien ce n'est pas ça qui vas empêcher que ton code PHP soit d'abord recompilé en bytecode puis que ce bytecode soit exécuté par PHP. Au mieux tu dois pouvoir faire en sorte de garder ton bytecode afin d'éviter d'avoir à refaire la très lourde étape de compilaton (je n'ai jamais essayé et je ne sais aps si c'est possible)... mais quand on en est à faire un maximun d'économies ça reste pas top.
<?php
class Node
{
private $childNodes;
private $data;
public function __construct($data)
{
$this->data = (string)$data;
$this->childNodes = array();
}
public function display()
{
echo $this->data, PHP_EOL;
foreach ($this->childNodes as $child)
$child->display();
}
public function addChild(Node $child)
{
$this->childNodes[] = $child;
}
public function getNChild($n)
{
if (!is_int($n))
throw new Exception('whatever');
if ($n < 0 || $n >= sizeof($this->childNodes))
throw new Exception('whatever bis');
return $this->childNodes[$n];
}
}
$root = new Node('Root');
$root->addChild(new Node('toto'));
$root->addChild(new Node('titi'));
$root->addChild(new Node('tutu'));
$root->getNChild(1)->addChild(new Node('plop'));
$root->display();
?>
#include
#include <vector>
class Node
{
private:
std::string _data;
std::vector<Node> _childNodes;
Node() {};
public:
Node(const std::string data) { _data += data; };
Node(const Node &cp)
{
if (&cp != this)
*this = cp;
};
~Node() {};
Node &operator=(const Node &r)
{
_data = r._data;
_childNodes = r._childNodes;
return *this;
};
void display()
{
std::cout << _data << std::endl;
for (unsigned int i = 0; i < _childNodes.size(); i++)
_childNodes[i].display();
};
void addChild(const Node &child)
{
_childNodes.push_back(child);
};
void addChild(const Node *child)
{
_childNodes.push_back(*child);
};
Node &getNChild(unsigned int n)
{
if (n >= _childNodes.size())
throw std::exception();
return _childNodes[n];
};
};
int
main()
{
Node root("Root");
root.addChild(new Node("toto"));
root.addChild(new Node("titi"));
root.addChild(new Node("tutu"));
root.getNChild(1).addChild(new Node("plop"));;
root.display();
return 0;
}
tycho@uraniborg-> g++ plop.cpp
tycho@uraniborg-> time ./a.out >/dev/null
real 0m0.005s
user 0m0.000s
sys 0m0.004s
tycho@uraniborg-> time php plop.php >/dev/null
real 0m0.103s
user 0m0.068s
sys 0m0.028s
Là ya pas photo, quand on est rendu à faire des économies de bouts de ficelle faut vraiment changer de langage, PHP c'est pas fait pour la perf.
... et encore j'ai pas mis de flags d'optimisation.
Allez on va dire que c'est peut être juste parce que PHP est un peu lourd au lancement, testons avec juste une petite boucle en plus (l'ajout du noeud "plop" effectué 1000 fois au lieux de 1 seule)
tycho@uraniborg-> g++ plop.cpp
tycho@uraniborg-> time ./a.out >/dev/null
real 0m0.008s
user 0m0.008s
sys 0m0.000s
tycho@uraniborg-> time php plop.php >/dev/null
real 0m0.132s
user 0m0.112s
sys 0m0.012s
Ba en fait non, même juste pour tourner PHP c'est vraiment lent...