Coder Social home page Coder Social logo

php-node-bubblesort-test's Introduction

Bubblesort Speed Comparison

There's a article detailing that PHP is slower than NodeJS where the author attempts to use bubble-sort as the base comparison.

However, I feel his implementation is flawed, thus, I'm using one from an algorithms and datastructures paper on bubblesort. I've adapted the psuedocode to PHP and NodeJS and have found PHP to be much faster than Node when the array contains less than 200 elements.

[PHP] array contains 197 elements, execution time: 2.32089 ms
[V8] array contains 197 elements, execution time: 2.520061 ms

After about 220 elements, PHP becomes increasingly slower

[PHP] array contains 226 elements, execution time: 3.03365 ms
[V8] array contains 226 elements, execution time: 2.40477 ms

At 1,000 elements PHP is 5 times slower than Node

php bubble-sort-v2.php; node bubble-sort-v2.js; # run tests back to back
[PHP] array contains 1002 elements, execution time: 223.782218 ms

[V8] array contains 1002 elements, execution time: 4.937916 ms

If I've made a mistake in time calculations, please let me know, and I'll correct the issue.

Thanks!

php-node-bubblesort-test's People

Contributors

gitkearney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-node-bubblesort-test's Issues

I think there's a flaw in your methodology

I've set up a fork to demonstrate what I'm talking about. You can see it here:

https://github.com/ryantate13/php-node-bubblesort-test/

In my test runs PHP is consistently ~20x slower than Node. I set up a script to cycle through 100 test runs and take the min time of each set per platform.

If you clone my repo and run make test you can see it in action. PHP is v7.2.18, Node is v10.14.2.

time elapsed (nanoseconds) for php is 133,391,141
time elapsed (nanoseconds) for node is 7,014,987

Node can go 3x faster

It might sound a bit like sorcery but if you will traverse array backwards you can achieve up to 3 times better performance. The only difference is to use this construction:

for ( var i = array.length; i--; ) {
   (...)
}

There has been prolonged discussion about it on GitHub: https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript
and here you can find related test:
http://jsbench.github.io/#67b13d4e78cdd0d7a7346410d5becf12

I didn't check it in node though but in browser... :
https://jsbench.me/j0jw6pygqu/1

Also as you can see in tests you can gain performance on declaring the data type for array.

Wonder how much improvements can be gain in PHP and if running on node will make the difference.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.