Jump to content

saving ram


mctrivia

Recommended Posts

i have made 2 classes. 

 

the first "point" has an array of 3 floats and some functions to help manipulate the values

the second "triangle" has an array of 3 points.

 

in this way if i want to make a tetrahedron i can make 4 points and assign them to 4 triangles.

 

problem is for some reason each triangle point group seems to use 1k.  any idea why?

Link to comment
Share on other sites

i guess my question is what makes an object require ram?  i would think point would need only enough space to store the 3 floats and triangle need only enough space to hold the 3 pointers.  my math says

80 bit*3 for floats+64bit*3 for pointers=54 bytes but 1000 are being used.

Link to comment
Share on other sites

Without knowing HOW it is coded this is just a hypothetical discussion. I can write two scripts that produce the exact same results but one uses exponentially more memory.

 

You say you are using a class, right? So just initiating the class will take memory. I have no idea how you are running this or what output is produced. Are you running the client and the server on the same machine?

 

This is the PHP Help forum where people post requests asking for help with their code. You have not provided code nor asked for help. If you are not going to post code, then this should be moved to the "Application Design" forum which includes "Programming theory, database design, performance, etc."

Link to comment
Share on other sites

class Point {

  protected $v=array();

  public function __construct() {
    $this->id=++$_GET['zzz_library_model_lastpoint'];
    if (func_num_args()==0) {
      $this->v=array(0,0,0);
    } elseif (func_num_args()==1) {
      $this->v=func_get_arg(0);
    } elseif (func_num_args()==3) {
      $this->v=array(func_get_arg(0),func_get_arg(1),func_get_arg(2));
    }
  }

  public function __get($name) {
    switch (strtolower($name)) {
      case 'x':
        return $this->v[0];
        break;
      case 'y':
        return $this->v[1];
        break;
      case 'z':
        return $this->v[2];
        break;
      case 'v':
        return $this->v;
        break;
      case 'id':
        return $this->id;
        break;
    }
  }

  public function __set($name,$value) {
    switch (strtolower($name)) {
      case 'x':
        $this->v[0]=$value;
        break;
      case 'y':
        $this->v[1]=$value;
        break;
      case 'z':
        $this->v[2]=$value;
        break;
      case 'v':
        $this->v=$value;
        break;
    }
  }

  public function __toString() {
    return pack("f",$this->v[0]) . pack("f",$this->v[1]) . pack("f",$this->v[2]);
  }

  public function rotate($theta,$axis) {  //theta in rads use deg2rad() if you want deg.
    switch (strtolower($axis)) {
      case 'x':
        $x=cos($theta)*$this->v[1]-sin($theta)*$this->v[2];
        $y=sin($theta)*$this->v[1]+cos($theta)*$this->v[2];
        $this->v[1]=$x;
        $this->v[2]=$y;
        break;
      case 'y':
        $x=cos($theta)*$this->v[2]-sin($theta)*$this->v[0];
        $y=sin($theta)*$this->v[2]+cos($theta)*$this->v[0];
        $this->v[2]=$x;
        $this->v[0]=$y;
        break;
      case 'z':
        $x=cos($theta)*$this->v[0]-sin($theta)*$this->v[1];
        $y=sin($theta)*$this->v[0]+cos($theta)*$this->v[1];
        $this->v[0]=$x;
        $this->v[1]=$y;
        break;
    }
  }

  public function translate($x,$y,$z) {
    $this->v[0]+=$x;
    $this->v[1]+=$y;
    $this->v[2]+=$z;
  }

  public function scale($x,$y=null,$z=1) {
    if ($y==null) {
      $y=$x;
      $z=$x;
    }
    $this->v[0]*=$x;
    $this->v[1]*=$y;
    $this->v[2]*=$z;
  }
}


// ************************************************************************************************************
class Triangle {

  protected $v=array();

  public function __construct() {
    if (func_num_args()==0) {
      $this->v=array(new Point(),new Point(),new Point());
    } elseif (func_num_args()==3) {
      $this->v=array(func_get_arg(0),func_get_arg(1),func_get_arg(2));
    } elseif (func_num_args()==9) {
      $this->v=array(
	new Point(func_get_arg(0),func_get_arg(1),func_get_arg(2)),
	new Point(func_get_arg(3),func_get_arg(4),func_get_arg(5)),
	new Point(func_get_arg(6),func_get_arg(7),func_get_arg()
);
    }
    return !(($v[0]==$v[1]) || ($v[0]==$v[2]) || ($v[1]==$v[2]));
  }

  public function __set($name,$value) {
    $t=substr($value,1,1);
    switch (substr(strtolower($name),0,1)) {
      case 'x':
        $this->v[$t]->x=$value;
        break;
      case 'y':
        $this->v[$t]->y=$value;
        break;
      case 'z':
        $this->v[$t]->z=$value;
        break;
      case 'v':
        $this->v[$t]=$value;
        break;
    }
  }

  public function __get($name) {
    if (strlen($name>1)) {
      $t=substr($value,1,1);
    } else {
      $t=0;
    }
    switch (substr(strtolower($name),0,1)) {
      case 'x':
        return $this->v[$t]->x;
        break;
      case 'y':
        return $this->v[$t]->y;
        break;
      case 'z':
        return $this->v[$t]->z;
        break;
      case 'v':
        return $this->v[$t];
        break;
      case 'p':
        return array_merge($this->v[0]->v,$this->v[1]->v,$this->v[2]->v);
        break;
      case 'n':
        return $this->getNormal();
        break;
    }
  }

  public function __toString() {
    $n=$this->getNormal();
    return pack("f",$n[0]).pack("f",$n[1]).pack("f",$n[2]).$this->v[0].$this->v[1].$this->v[2].pack("S",0);
  }

  protected function getNormal() {
    $a0=$this->v[2]->x-$this->v[0]->x;
    $a1=$this->v[2]->y-$this->v[0]->y;
    $a2=$this->v[2]->z-$this->v[0]->z;
    $b0=$this->v[1]->x-$this->v[0]->x;
    $b1=$this->v[1]->y-$this->v[0]->y;
    $b2=$this->v[1]->z-$this->v[0]->z;

    $x=$a1*$b2-$a2*$b1;
    $y=$a2*$b0-$a0*$b2;
    $z=$a0*$b1-$a1*$b0;

    $l=sqrt($x*$x+$y*$y+$z*$z);
    $x/=$l;
    $y/=$l;
    $z/=$l;

    return array($x,$y,$z);
  }

}

Link to comment
Share on other sites

i guess my question is what makes an object require ram?  i would think point would need only enough space to store the 3 floats and triangle need only enough space to hold the 3 pointers.  my math says

80 bit*3 for floats+64bit*3 for pointers=54 bytes but 1000 are being used.

 

Ok, looking at your code there are a heck of a lot more variables than what you state above. Just because your class is returning x number variables doesn't mean that more variables were created (and stored in memory).

 

Case in point:

  protected function getNormal() {
    $a0=$this->v[2]->x-$this->v[0]->x;
    $a1=$this->v[2]->y-$this->v[0]->y;
    $a2=$this->v[2]->z-$this->v[0]->z;
    $b0=$this->v[1]->x-$this->v[0]->x;
    $b1=$this->v[1]->y-$this->v[0]->y;
    $b2=$this->v[1]->z-$this->v[0]->z;

    $x=$a1*$b2-$a2*$b1;
    $y=$a2*$b0-$a0*$b2;
    $z=$a0*$b1-$a1*$b0;

    $l=sqrt($x*$x+$y*$y+$z*$z);
    $x/=$l;
    $y/=$l;
    $z/=$l;

    return array($x,$y,$z);
  }

 

That function returns three variables, but there are ten total variables generated within that function ($a1, $a2, $a3, etc.). I also suspect that some memory is used as overhead in performing the mathematical operations that isn't necessarily freed up immediately.

Link to comment
Share on other sites

Without knowing how you are tracking memory usage and at what time there are any number of reasons why you are getting the results you have stated. Besides, memory usage is a tricky thing. Also, just because a function/script ends doesn't mean the memory is instantaneously released.

 

Take a look at this post with some relevant info: http://stackoverflow.com/questions/1145775/why-does-this-simple-php-script-leak-memory

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.