DaveChild / Text-Statistics

Generate information about text including syllable counts and Flesch-Kincaid, Gunning-Fog, Coleman-Liau, SMOG and Automated Readability scores.
https://readable.com/
BSD 2-Clause "Simplified" License
447 stars 107 forks source link

Increase performance #8

Closed xeoncross closed 10 years ago

xeoncross commented 11 years ago

Processing the same text over-and-over is not a good idea. Just process it once and store it.

 class TextStatistics {

    protected $text;
    protected $strEncoding = ''; // Used to hold character encoding to be used by object, if set

    /**
     * Constructor.
     *
     * @param string  $strEncoding    Optional character encoding.
     * @return void
     */
    public function __construct($text = NULL) {
        if($text) {
            $this->setText($text);
        }
    }

    /**
     * Set the text to parse
     */
    public function setText($text) {
        $this->strEncoding = mb_detect_encoding($text);
        $this->text = $this->clean_text($text);
    }

    /**
     * Fetch the current object text
     */
    public function getText() {
        return $this->text;
    }

    /**
     * Fetch the current object encoding
     */
    public function getEncoding() {
        return $this->strEncoding;
    }

    /**
     * Gives the Flesch-Kincaid Reading Ease of text entered rounded to one digit
     * @param   strText         Text to be checked
     */
    public function flesch_kincaid_reading_ease() {
        $strText = $this->text;

....

After making this simple change the processing time for me on a small document dropped from 0.16 seconds to 0.11 seconds do to the reduced clean_text calls.

jrfnl commented 10 years ago

I've implemented this in a slightly different way in commit https://github.com/jrfnl/Text-Statistics/commit/2dda617dfad71724cbe43c42761a314504d566be which is included in pull request #16.

DaveChild commented 10 years ago

Nice solution, @jrfnl.