Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

2019-06-27

Language performance test

Lower value shows better performance

  • C
    #include <stdio.h>
    
    int fib(int n) {
        if(n == 1) return 0;
        if(n == 2) return 1;
        return fib(n - 2) + fib(n - 1);
    }
    
    int main() {
        int i = 0;
        for(i = 1; i <= 40; i++) {
            printf("%d %d\n", i, fib(i));
        }
        printf("\nC\n");
        return 0;
    }
    
  • Java
    public class Fib {
     
        public static int calc(int n) {
            if(n <= 1) return 0;
            if(n == 2) return 1;
            return Fib.calc(n - 2) + Fib.calc(n - 1);
        }
        public static void main(String[] args) {
            for(int i = 1; i <= 40; i++) {
                System.out.println(i + " " + Fib.calc(i));
            }
            System.out.println("\nJAVA");
        }
     
    }
    
  • JavaScript
    #!/usr/bin/env node
    
    let range = Array.from({length: 40}, (x, i) => i+1);
    let fib = n => n === 1 ? 0 : n === 2 ? 1 : fib(n - 2) + fib(n - 1);
    
    range.forEach( i => console.log(i, fib(i)) );
    console.log("\nJavaScript");
    
  • PHP
    #!/usr/bin/env php
    <?php
    function fib(int $n) {
        if ($n === 1) return 0;
        if ($n === 2) return 1;
        return fib($n - 2) + fib($n - 1);
    }
    
    foreach (range(1, 40) as $i) {
        echo $i, ' ', fib($i), PHP_EOL;
    }
    echo "\nPHP\n";
    
  • Python
    #!/usr/bin/env python
    
    def fib(n):
      if n == 1:
        return 0
      elif n == 2:
        return 1
      return fib(n - 2) + fib(n - 1)
    
    if __name__ == "__main__":
      for i in range(1, 40 + 1):
        print(i, fib(i))
      print("\nPYTHON")
    
  • Ruby
    #!/usr/bin/env ruby
    
    def fib(n)
      return 0 if n == 1
      return 1 if n == 2
      return fib(n - 2) + fib(n - 1)
    end
    
    (1..40).each do |i|
      puts "#{i.to_s} #{fib(i).to_s}"
    end
    puts "\nRUBY"
    

2013-08-23

How to run NginX/PHP/MySQL on Mac


Needed to run DRUPAL on Mac
  1. Install XCode
  2. Install XCode Command Line Tools
  3. XCode > Preferences > Downloads > Command Line Tools
  4. Install Homebrew - Similar to MacPorts but better
  5. $ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
    $ brew doctor
    $ brew tap josegonzalez/homebrew-php
    $ brew tap homebrew/dupes
  6. Install MySQL
  7. $ brew install mysql
    $ unset TMPDIR
    $ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
    $ sudo mv /usr/local/opt/mysql/my-new.cnf /usr/local/opt/mysql/my.cnf
    $ cp `brew --prefix mysql`/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
    $ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  8. Install NginX
  9. $ brew install nginx
    $ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/
    $ sudo sed -i -e 's/`whoami`/root/g' `brew --prefix nginx`/homebrew.mxcl.nginx.plist
    $ sudo mkdir /var/log/nginx/
  10. Install PHP
  11. $ brew install --without-apache --with-fpm --with-mysql php54
    $ sudo cp `brew --prefix php54`/homebrew-php.josegonzalez.php54.plist  /Library/LaunchAgents/
    $ sudo launchctl load -w /Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist

2013-01-18

How to setup virtual directory with PHP support on Nginx

Setup virtual directory with PHP support.
location /phpmyadmin {
    alias /opt/phpmyadmin/;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

When to use mutliple virtual directories, seperate and include the configuration.
Create /etc/nginx/conf.d/php.inc
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

Include above file into the virtual directory configuration.
location /phpmyadmin {
    alias /opt/phpmyadmin/;
    include /etc/nginx/conf.d/php.inc;
}