LiamM32 / Eurovision_Condorcet

A program to count votes for the Eurovision Song Contest using a redesigned voting system.
1 stars 2 forks source link

Benchmarking feature #11

Closed LiamM32 closed 1 year ago

LiamM32 commented 1 year ago

A way to see how long it took to run a particular voting method, so that I can see how much code changes affected the time taken.

LiamM32 commented 1 year ago

There is a new branch called 'benchmark' that has a benchmark in tests/benchmark.php. From commit 3690467, you can run the benchmark with php tests/benchmark.php some-votes.cvotes. It will run each method listed 9 times, and then echo the average and maximum time for each.

I also made some new variants of EurovisionCondorcet in commit c31dcc3 to compare the performance. However, I put all the new classes in traits in one file. I much, much, much prefer this to having 5 separate files for them, but I couldn't manage to get them recognized in Init.php without them complying with the PSR-4 convention.

If there is any, any way to solve this without splitting it into multiple files, I would be very happy to know.

julien-boudry commented 1 year ago

PSR-4 is a good standard, better it's to follow it. It's also compliant with code analytic and refactors tools. And other programmers can understand your code.

If bot, you can do something like that:

<?php

declare(strict_types=1);

namespace EurovisionVoting;

use CondorcetPHP\Condorcet\Condorcet;
use EurovisionVoting\Method\EurovisionSchulze;
use EurovisionVoting\Method\EurovisionSchulze2;
use EurovisionVoting\Method\EurovisionSchulze3;
use EurovisionVoting\Method\EurovisionSchulze1b;
use EurovisionVoting\Method\EurovisionSchulze1c;
use EurovisionVoting\Method\EurovisionSchulze1d;

require_once __DIR__ . '/Method/minor_variants.php';

class Init {
    public static function registerMethods(): void {
        Condorcet::addMethod(EurovisionSchulze::class);
        Condorcet::addMethod(EurovisionSchulze2::class);
        Condorcet::addMethod(EurovisionSchulze3::class);
        Condorcet::addMethod(EurovisionSchulze1b::class);
        Condorcet::addMethod(EurovisionSchulze1c::class);
        Condorcet::addMethod(EurovisionSchulze1d::class);
    }
}

But I strongly discourage that.

LiamM32 commented 1 year ago

I see what you mean. I'll follow PSR-4 in most cases, though this is just a set of minor variants that aren't here to stay long-term, so I would prefer not to have the clutter.

However, the edits that you provided still don't work. I still get the same output:

PHP Fatal error:  Uncaught CondorcetPHP\Condorcet\Throwable\AlgorithmException: The voting algorithm is not available: no class found for 'EurovisionVoting\Method\minor_variants\EurovisionSchulze1b' in /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/Condorcet.php:189
Stack trace:
#0 /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/Condorcet.php(170): CondorcetPHP\Condorcet\Condorcet::testMethod()
#1 /home/liam/src/Eurovision_Condorcet/src/Init.php(20): CondorcetPHP\Condorcet\Condorcet::addMethod()
#2 /home/liam/src/Eurovision_Condorcet/tests/benchmark.php(14): EurovisionVoting\Init::registerMethods()
#3 {main}
  thrown in /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/Condorcet.php on line 189
julien-boudry commented 1 year ago

Working well here, on the benchmark branch.

LiamM32 commented 1 year ago

Thank you. I got it working. Before I must have not copied what you wrote completely. I admit that noncompliance with PSR-4 can make things harder, given it's easier to make a mistake.

I have pushed a new commit.