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

**URL:** https://forum.kirupa.com/t/n-iterations-of-k-function-calls-or-k-function-calls-of-n-iterations/304300
**Category:** classic bestof
**Created:** [February 5, 2010, 2:34am UTC](https://forum.kirupa.com/t/n-iterations-of-k-function-calls-or-k-function-calls-of-n-iterations/304300 "2010-02-05T02:34:14Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![KomodoDave](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@KomodoDave](https://forum.kirupa.com/u/KomodoDave)
#### Post date: [February 5, 2010, 2:34am UTC](https://forum.kirupa.com/t/n-iterations-of-k-function-calls-or-k-function-calls-of-n-iterations/304300/1 "2010-02-05T02:34:14Z")

</div>

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:

```auto

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.

```auto

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
