Execution Speed (compare to C++)
(Redirected from Execution Speed (compare to C ))
It is not completely trivial to compare the execution speed of different programming languages, because some problems may suit one language better than another. Nevertheless such comparison is interesting.
This article describes a little simple comparison of Visual Prolog and Microsoft C++. The comparison does not claim any scientific accuracy; it is just supposed to give an indication.
The classical Towers of Hanoi problem is used for the test, the actual writing of the result to the standard output stream is skipped, because otherwise the test take nearly the same time because all the time is spend in drawing and scrolling the console window.
The test is carried out with Visual Prolog 7.2 and Microsoft C++ (Visual Studio 2005).
Visual Prolog run
class main predicates run : core::runnable. end class main implement main clauses run():- console::init(), profileTime::init(), profileTime::start_pr("hanoi"), hanoi(25, "a", "b", "c"), profileTime::stop_pr("hanoi"), profileTime::printAndReset(stdio::getOutputStream()). class predicates hanoi : (positive N, string SourcePole, string AuxPole, string TargetPole). clauses hanoi(0, _SourcePole, _AuxPole, _TargetPole) :- !. hanoi(N, SourcePole, AuxPole, TargetPole) :- hanoi(N-1, SourcePole, TargetPole, AuxPole), /* stdio::writef("Move from % to %\n", SourePole, TargetPole),*/ hanoi(N-1, AuxPole, SourcePole, TargetPole). end implement main goal mainExe::run(main::run).
The result was this
Steps = 134217727 | Predicate | Calls | AvgNet | Netto | Average | Total | +-------------------+-------+------------+------------+------------+-----------+ | hanoi | 1 | 5.6989841 | 5.698984 | 5.698984 | 5.698984| +-------------------+-------+------------+------------+------------+-----------+
C++ run
#include "stdafx.h" typedef wchar_t* string; static int steps = 0; void hanoi(int N, string A, string B, string C) { if (N > 0) { hanoi(N-1, A, C, B); // move a disk from A to C steps++; hanoi(N-1, B, C, A); } } int _tmain(int argc, _TCHAR* argv[]) { clock_t start = clock(); hanoi(27, L"left", L"middle", L"right"); clock_t finish = clock(); wprintf(L"Steps = %d\n", steps); double elapsed_time = (finish - start) / 1000.0; printf( "\nProgram takes %.2f seconds.\n", elapsed_time ); return 0; }
In Debug mode this program produced:
Steps = 134217727 Program takes 38.45 seconds.
In Release mode it produced:
Steps = 134217727 Program takes 1.95 seconds.
Summary
Test | Time | Relative |
---|---|---|
C++ Debug | 38.45 sec | 675% |
Visual Prolog | 5.70 sec | 100% |
C++ Release | 1.95 sec | 34% |
So the Release version of the C++ program is around 3 times faster than the Visual Prolog program which is 6.75 times faster than the Debug version of the C++ program.
So this Visual Prolog program though significantly slower than the optimized C++ program is however closer to the optimized C++ program than to the un-optimized C++ program.