# Speed question: 2D array vs. Table

**URL:** <https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707>\
**Category:** Beginners\
**Created:** [March 13, 2022, 2:12pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707 "2022-03-13T14:12:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2022, 2:12pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707/1 "2022-03-13T14:12:10Z")

</div>

Excuse my question.

I need to fill an 2D array with Strings.

Then I want to look up / search certain strings in a precise column.

Would the access be faster using a table with 30  
Columns instead of a 2D array?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 13, 2022, 2:44pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707/2 "2022-03-13T14:44:29Z")

</div>

> [@Chrisir](#):
>
> Would the access be faster using a Table with 30  
> columns instead of a 2D array?

Using a Table container we would invoke its **findRow()** method for a unique String search:

> **[findRow() / Reference](https://processing.org/reference/Table_findRow_.html)**
>
> Finds the first row in the Table that contains the value provided, and returns a reference to that row. Even if multiple rows are possible matches, only the first matching row is returned. The …

> <https://github.com/processing/processing/blob/processing-0270-3.5.4/core/src/processing/data/Table.java#L3699-L3710>

Which in turn invokes method **findRowIndex()**:

> <https://github.com/processing/processing/blob/processing-0270-3.5.4/core/src/processing/data/Table.java#L3592-L3620>

A 2d array custom column lookup would be faster of course.  
I just dunno how much faster compared to the Table’s algorithm though.  
It’s a classical convenience vs. speed!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2022, 4:25pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707/3 "2022-03-13T16:25:19Z")

</div>

> [@GoToLoop](#):
>
> It’s a classical convenience vs. speed!

Thank you.

I didn’t know that 2D array would be faster. I would habe to write a for loop to search the String?

Anyway, to build the table from scratch from the data would also be much work.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 13, 2022, 5:04pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707/4 "2022-03-13T17:04:45Z")

</div>

> [@Chrisir](#):
>
> I would have to write a for loop to search the String?

I’ve just done 1 for you now. However I haven’t tested it yet: ⚠

```auto
static final String[]
  findRow(final String[][] table, final String word, final int col)
{
  final int row = findRowIndex(table, word, col);
  return row >= 0? table[row] : null;
}

static final int
  findRowIndex(final String[][] table, final String word, int col)
{
  if (table != null && word != null && (col = abs(col)) < table[0].length)
    for (int row = 0; row < table.length; ++row)
      if (word.equals(table[row][col])) return row;

  return -1;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2022, 5:14pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707/5 "2022-03-13T17:14:48Z")

</div>

I guess when you say 2D array is faster I believe you and I write a for loop to search the 2D array

Or was it an misunderstanding?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 13, 2022, 5:19pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707/6 "2022-03-13T17:19:33Z")

</div>

Compare the lookup algorithm I did for a 2D array of strings and the Table’s implementation. 👓

I didn’t benchmark them either; but it seems like mine would win the race. 🏇

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2022, 5:37pm UTC](https://discourse.processing.org/t/speed-question-2d-array-vs-table/35707/7 "2022-03-13T17:37:36Z")

</div>

Now I understand. Thanks! The second code is for a 2D array.

Thank you!
