N iterations of K function calls, or K function calls of N iterations?

Hi guys,

I wish to implement a constraints solver in a physical simulation, and wish to model each constraint as a Function object.

I’m wondering whether it’s best to have each constraint operate on a full set of data, thus code will be in the form:


public function constraint1(dataset:Vector.<T>)
{
  for (var i:int = 0; i < dataset.length; ++i)
    ; // do stuff
}

public function constraint2(dataset:Vector.<T>)
{
  for (var i:int = 0; i < dataset.length; ++i)
    ; // do stuff
}

public function satisfyConstraints(dataset:Vector.<T>)
{
  constraint1(dataset);
  constraint2(dataset);
}

…or else to have each constraint operate on a lone dataset member, i.e.



public function constraint1(data_i:T)
{
  // do stuff
}

public function constraint2(data_i:T)
{
  // do stuff
}

public function satisfyConstraints(dataset:Vector.<T>)
{
  for (var i:int = 0; i < dataset.length; ++i)
  {
    constraint1(dataset*);
    constraint2(dataset*);
  }
}

Assume there are K constraints (thus there are K constraint Function objects instantiated), with a dataset of size N.

In the first version there will be K function calls, with KN iterations in total.

In the second version there will be KN function calls, with N iterations in total.

Which will perform best, judging from your experience?

Many thanks,

David