Skip to content
sentiment-analysis
Menu

Performance

The package is cheap to call. Build the analyzer once, then loop over your texts:

use Risan\Sentiment\Analyzer;

$reviews = ['Great phone!', 'The battery is terrible.'];

$analyzer = new Analyzer();

foreach ($reviews as $review) {
    $label = $analyzer->analyze($review)->label;
}

Each call makes one pass over the words and looks each word up in constant time. The lexicons are stored in a form PHP can share between requests.

Benchmarks

The numbers come from benchmarks/run.php in the repository, on PHP 8.5.11 (CLI, Docker on a Windows laptop with WSL2). They are single runs, so treat them as a rough guide. Repeated runs vary by 10% or more.

Text Words Analyses per second, OPcache on Analyses per second, OPcache off
Tweet, English 17 about 80,000 about 66,000
Review, English 106 about 14,000 about 12,000
Long text, English 2,120 about 770 about 650
Tweet, Indonesian 10 about 66,000 about 57,000
Review, Indonesian 93 about 14,000 about 12,000
Long text, Indonesian 1,860 about 770 about 670

The peak memory of the whole benchmark process was about 2.2 MB with OPcache on and about 3 MB with it off. Both languages were loaded.

To see numbers for your own setup, run the benchmark:

php -d opcache.enable_cli=1 benchmarks/run.php

Turn on OPcache

The lexicons are plain PHP files that return an array. With OPcache, PHP compiles such a file once and keeps the array in shared memory. Every request then reads it with no parsing and no copying.

Without OPcache, PHP parses the file once per process. A long-running worker pays this cost once. A classic request-per-process setup pays it on every request.

Most PHP installs have OPcache on, but not for the CLI. In production with PHP-FPM, check that it is on:

opcache.enable=1
; Reset the cache when you deploy. This saves a stat() per file.
opcache.validate_timestamps=0

For CLI jobs such as queue workers or imports, turn it on yourself:

php -d opcache.enable_cli=1 artisan queue:work

Get the most out of it

  • Reuse the analyzer. Sentiment::analyze() already shares one default analyzer per language. If you customize with withWords() or withThreshold(), build the analyzer once and reuse it.
  • Loop freely. Each call is independent and keeps no state. One process can analyze thousands of texts.
  • Keep texts short. The work grows with the number of words. For long documents, analyze per sentence. The quality is better and the total cost is the same.
  • Plain ASCII text is the fastest. Text with accents, emoji or other scripts takes extra checks per word.

Memory

A lexicon loads once per process and per language. Analyzing a text only allocates its word list, so memory stays flat however many texts you analyze.

Without OPcache, loading a lexicon costs about 1 MB for English and 0.5 MB for Indonesian per process. The whole benchmark process peaks at about 3 MB. With OPcache, the lexicons live in shared memory and the cost per process is a few KB.

Edit this page on GitHub