C/C++ library from PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oguz Divilioglu

    C/C++ library from PHP

    Hello;
    i want to use linked list classes formed in C++
    and use them after including my own dinamic library written in C++
    (i'm using Linux - shared object - .so-)
    the problem is:
    - do i have to do it only and only with the help of ZEND APIs?
    is there not any standart way to do this inside PHP?
    - if not with ".so" s, any linked list classes in PHP?(i want to use
    linked lists because of the high dataload to process and the memory
    doesnt allow the array structure after some extent)

    Thank You
    Oğuz Divilioğlu
  • Marc Quinton

    #2
    Re: C/C++ library from PHP

    Oguz Divilioglu wrote:
    [color=blue]
    > - if not with ".so" s, any linked list classes in PHP?(i want to use
    > linked lists because of the high dataload to process and the memory
    > doesnt allow the array structure after some extent)[/color]

    linked list wont be lighter ..

    Comment

    • Marc Quinton

      #3
      Re: C/C++ library from PHP

      Oguz Divilioglu wrote:
      [color=blue]
      > - if not with ".so" s, any linked list classes in PHP?(i want to use
      > linked lists because of the high dataload to process and the memory
      > doesnt allow the array structure after some extent)[/color]

      each cells of an array contain some data overhead, so handling large
      array become difficult and memory consuming.

      you could pearhap transform array to a binary string containing
      all elements and implement I/O methods and a seek() method
      to read and extract compated data.

      This could especialy done if each records are same length.

      Comment

      • Marc Quinton

        #4
        Re: C/C++ library from PHP

        Marc Quinton wrote:[color=blue]
        > Oguz Divilioglu wrote:
        >[color=green]
        >> - if not with ".so" s, any linked list classes in PHP?(i want to use
        >> linked lists because of the high dataload to process and the memory
        >> doesnt allow the array structure after some extent)[/color]
        >
        >
        > each cells of an array contain some data overhead, so handling large
        > array become difficult and memory consuming.
        >
        > you could pearhap transform array to a binary string containing
        > all elements and implement I/O methods and a seek() method
        > to read and extract compated data.
        >
        > This could especialy done if each records are same length.[/color]


        here is a sample test program :

        mq > ~/usr/stow/php-4.3.10/bin/php -q array-large.php
        memory usage : 808/80.8
        memory usage : 5480/54.8
        memory usage : 54424/54.424
        memory usage : 611368/61.1368
        memory usage : 6110120/61.1012


        <?php

        function _memory_get_usa ge(){
        return memory_get_usag e();

        # $my_pid = getmypid();
        # return `ps -eo%mem,rss,pid | grep $my_pid`;
        }

        function test($count){

        $start = _memory_get_usa ge();

        $list = array();

        for($i=0 ; $i < $count ; $i++){
        $list[] = $i;
        }

        $end = _memory_get_usa ge();
        unset($list);

        $usage = $end - $start;
        $per_unit = $usage / $count;

        echo "memory usage : " . $usage . '/' . $per_unit . "\n";


        }

        test(10);
        test(100);
        test(1000);
        test(10000);
        test(100000);

        ?>

        Comment

        • Marc Quinton

          #5
          Re: C/C++ library from PHP

          Marc Quinton wrote:
          [color=blue][color=green]
          >> This could especialy done if each records are same length.[/color][/color]


          a far more complete test with a nearly complete class
          usable just as a array bag. But there are some error
          in get(), unpack(), dump(). Those errors should not
          be very difficult to correct.

          have fun ...


          <?php

          error_reporting (E_ALL);

          function _memory_get_usa ge(){
          return memory_get_usag e();

          # $my_pid = getmypid();
          # return `ps -eo%mem,rss,pid | grep $my_pid`;
          }


          class LargeArray{
          var $container;
          var $len;
          var $rec_len;

          function LargeArray($rec _len = 4){
          $this->container = '';
          $this->len = 0;
          $this->rec_len = $rec_len;
          }

          function add($val){
          $this->container .= $this->pack($val);
          $this->len++;
          }

          function dump(){
          if($i=0)
          return;
          for ($i=0 ; $i<$this->len ; $i++){
          echo "$i" . $this->get($i) . "\n";
          }
          }

          function get($pos){
          $data = substr($this->container, $pos * $this->rec_len, $this->rec_len);
          echo "data = '$data'\n";
          return $this->unpack($data );
          }

          # pack and unpack need to know about record len
          # or be overriden by a subclass ...
          # this one works for integers.
          #
          function pack($val){
          return pack('N', $val);
          }

          function unpack($data){
          return unpack('N', $data);
          }

          function free(){
          unset($this->container);
          }
          }

          function test3($count){
          $start = _memory_get_usa ge();

          $array = new LargeArray(4);

          for($i=0 ; $i < $count ; $i++){
          $array->add($i);
          }

          $end = _memory_get_usa ge();
          $array->free();
          unset($array);

          $usage = $end - $start;
          $per_unit = $usage / $count;

          echo "memory usage : " . $usage . '/' . $per_unit . "\n";
          }

          function test1($count){

          $start = _memory_get_usa ge();

          $list = '';

          for($i=0 ; $i < $count ; $i++){
          $list .= pack('N', $i);
          }

          $end = _memory_get_usa ge();
          unset($list);

          $usage = $end - $start;
          $per_unit = $usage / $count;

          echo "memory usage : " . $usage . '/' . $per_unit . "\n";

          }


          function test2($count){

          $start = _memory_get_usa ge();

          $list = array();

          for($i=0 ; $i < $count ; $i++){
          $list[] = $i;
          }

          $end = _memory_get_usa ge();
          unset($list);

          $usage = $end - $start;
          $per_unit = $usage / $count;

          echo "memory usage : " . $usage . '/' . $per_unit . "\n";

          }

          $test = 55;
          $func = 'test' . $test;

          if(function_exi sts($func)){
          $func(10);
          $func(100);
          $func(1000);
          $func(10000);
          $func(100000);
          }

          # test array methods ...
          $array = new LargeArray(4);
          for($i=0 ; $i<10 ; $i++){
          $array->add($i*2);
          }

          $array->dump();
          $array->free();


          ?>

          Comment

          • R. Rajesh Jeba Anbiah

            #6
            Array memory usage benchmark (Was Re: C/C++ library from PHP)

            Marc Quinton wrote:[color=blue]
            > Marc Quinton wrote:[color=green]
            > > Oguz Divilioglu wrote:[/color][/color]
            <snip>[color=blue]
            > here is a sample test program :
            >
            > mq > ~/usr/stow/php-4.3.10/bin/php -q array-large.php
            > memory usage : 808/80.8
            > memory usage : 5480/54.8
            > memory usage : 54424/54.424
            > memory usage : 611368/61.1368
            > memory usage : 6110120/61.1012[/color]

            Very interesting. I couldn't think of the life without array:-(
            Anyway, do you have any suggestions or workarounds?

            --
            <?php echo 'Just another PHP saint'; ?>
            Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

            Comment

            • Marc

              #7
              Re: Array memory usage benchmark (Was Re: C/C++ library from PHP)

              R. Rajesh Jeba Anbiah a écrit :
              [color=blue]
              > Very interesting. I couldn't think of the life without array:-(
              > Anyway, do you have any suggestions or workarounds?[/color]

              yes, look at my preliminary LargeArray class.

              Comment

              • R. Rajesh Jeba Anbiah

                #8
                Re: Array memory usage benchmark (Was Re: C/C++ library from PHP)

                Marc wrote:[color=blue]
                > R. Rajesh Jeba Anbiah a écrit :
                >[color=green]
                > > Very interesting. I couldn't think of the life without array:-(
                > > Anyway, do you have any suggestions or workarounds?[/color]
                >
                > yes, look at my preliminary LargeArray class.[/color]

                You're using class and pack, but still faster than array? Really
                interesting. Will try to benchmark sometimes later.

                --
                <?php echo 'Just another PHP saint'; ?>
                Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                Comment

                • Marc Quinton

                  #9
                  Re: Array memory usage benchmark (Was Re: C/C++ library from PHP)

                  R. Rajesh Jeba Anbiah wrote:
                  [color=blue]
                  > You're using class and pack, but still faster than array? Really
                  > interesting. Will try to benchmark sometimes later.[/color]


                  here is a working class (for integers)

                  class LargeArray{
                  var $container;
                  var $len;
                  var $rec_len;

                  function LargeArray($rec _len = 4){
                  $this->container = '';
                  $this->len = 0;
                  $this->rec_len = $rec_len;
                  }

                  function add($val){
                  $this->container .= $this->pack($val);
                  $this->len++;
                  }

                  function dump(){
                  if($i=0)
                  return;
                  for ($i=0 ; $i<$this->len ; $i++){
                  $data = $this->get($i);
                  echo "$i - " . $data . "\n";
                  }
                  }

                  function get($pos){
                  $data = substr($this->container, $pos * $this->rec_len, $this->rec_len);
                  $data = $this->unpack($data );
                  # print_r($data);
                  return $data[1];
                  }

                  # pack and unpack need to know about record len
                  # or be overriden by a subclass ...
                  # this one works for integers.
                  #
                  function pack($val){
                  return pack('N', $val);
                  }

                  function unpack($data){
                  return unpack('N', $data);
                  }

                  function free(){
                  unset($this->container);
                  }
                  }

                  Comment

                  Working...