NEW: Eat cabbage soup over 300 times faster!

Ever since I released Profanity Filter, I’ve been wondering how I could make it faster. It could take over a minute to check a single post for profanity. Not any more! The following simple test was used on both the new and old versions of the censoring algorithm.

<?php

require_once '../../bb-load.php';

header( 'Content-Type: text/plain' );

echo 'Sentence: ', $_sentence = 'I really like this cabbaj soup. It tastes souper, like a kabaj should. I like soop very mush.', "\n";
echo 'Primary words: cabbage', "\n";
echo 'Secondary words: soup', "\n";
echo 'Whitelist: (empty)', "\n";
echo 'Mode: char', "\n";
echo 'Replacement: *', "\n";
echo "\n";

$start1 = microtime( true );
echo profanity_filter_censor_old( $_sentence, array(
    'words' => array( 'cabbage' ),
    'secondary_words' => array( 'soup' ),
    'whitelist' => array(),
    'type' => 'char',
    'replacement' => '*'
) );
$end1 = microtime( true );

echo "\nOld method: ", ( $end1 - $start1 ) * 1000, 'ms', "\n\n";

$start2 = microtime( true );
echo profanity_filter_censor( $_sentence, array(
    'words' => array( 'cabbage' ),
    'secondary_words' => array( 'soup' ),
    'whitelist' => array(),
    'type' => 'char',
    'replacement' => '*'
) );
$end2 = microtime( true );

echo "\nNew method: ", ( $end2 - $start2 ) * 1000, 'ms', "\n\n";

Here’s the result:

Sentence: I really like this cabbaj soup. It tastes souper, like a kabaj should. I like soop very mush.
Primary words: cabbage
Secondary words: soup
Whitelist: (empty)
Mode: char
Replacement: *

I really like this ****** ****. It tastes souper, like a ***** should. I like **** very mush.
Old method: 1275.486946106ms

I really like this ****** ****. It tastes souper, like a ***** should. I like **** very mush.
New method: 4.4679641723633ms

The numbers should speak for themselves. Go download the new version of Profanity Filter!

Advertisements