phildawson's avatar

do lua have any forums for asking why it's very slow? what was the reason again for using lua? could another language be used or lua refer off to a c++ script to do the crunching.

keevitaja's avatar

I'll port this lua implementation to php with array based queues. lets see then...

... lua is the scripting language mudlet uses.

Edit: btw the farthest room is 31111, it took my script 60 seconds...

But i am pretty sure, that my queues are slow. each time element is pulled the stack index is rebuild. it is grazy

@phildawson please check 31111 with php and on your mac...

phildawson's avatar

@keevitaja

Edit: btw the farthest room is 31111, it took my script 60 seconds...

Yeah that can't be right I'd expect a couple tenths of a second to be the longest.

So I've just extracted the code from running in Laravel (through homestead) and made a single php file to run from cli directly on the mac PHP 5.5.21 (cli).

The only difference is to build the adjacency list. No refactoring at all.

$db = new SQLite3('aardwolf.db');
$exits = $db->query('SELECT * FROM exits');

while($exit = $exits->fetchArray(SQLITE3_ASSOC)) {
    $graph[$exit['from_num']][$exit['command']] = $exit['to_num'];
}
52180 -> 41624
0.095257043838501 (previously 0.15206694602966)

52180 -> 31111  = (82)
0.20017910003662

Edit:

vagrant box on the same desktop iMac running PHP 7.0.0-dev to see if it could beat 0.2 and it did:

52180 -> 31111

0.16998098373413

52180 -> 41624

0.049558153152466

52180 -> 30483

0.0063119144439697

.

PHP 7 on a production server should ace these all back to a thousandth of a second. If you need it any quicker c++. Personally I'd fall to Amazon for crunching.

keevitaja's avatar

@phildawson Thank you!

I had time to play with it again. This time i tried also php, and results are bit better, but still not usable. This is the code i used. Running it from cli.

<?php

namespace Aardwolf\Controllers;

use SplQueue;
use SQLite3;

class SqlController extends Controller
{
    protected function bfs($graph, $start, $goal)
    {
        $queue = new SplQueue;
        $visited = [];

        $queue->enqueue($start);
        $visited[] = $start;

        while ($queue->count() > 0) {
            $node = $queue->dequeue();

            if ($node == $goal) return true;

            foreach ($graph[$node] as $exit) {
                if ( ! in_array($exit, $visited)) {
                    $visited[] = $exit;

                    if (isset($graph[$exit])) $queue->enqueue($exit);
                }
            }
        }

        return false;
    }

    public function test()
    {
        $db = new SQLite3('/home/user/aardtin/engine/aardwolf.db');

        $result = $db->query("SELECT * FROM exits");

        $graph = [];

        while($row = $result->fetchArray()) {
            $id = $row['from_num'];
            $graph[$id][] = $row['to_num'];
        }

        var_dump(count($graph));

        $t = time() + microtime();

        var_dump($this->bfs($graph, 52180, 41624));
        
        var_dump(time() + microtime() - $t);
    }
}

This takes about 4 seconds :P

So i guess my computer is way too slow. But as people are mudding on slower computers, it is not an option. BFS sucks!

Previous

Please or to participate in this conversation.