# Difference between loadPixels and get()

**URL:** <https://discourse.processing.org/t/difference-between-loadpixels-and-get/24491>\
**Category:** Beginners\
**Created:** [October 10, 2020, 4:20am UTC](https://discourse.processing.org/t/difference-between-loadpixels-and-get/24491 "2020-10-10T04:20:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [October 10, 2020, 4:20am UTC](https://discourse.processing.org/t/difference-between-loadpixels-and-get/24491/1 "2020-10-10T04:20:23Z")

</div>

As the title implies just after a bit of info.

Looking through some examples I came across the histogram image sketch which makes use of get to retrieve pixel values.

```auto
for (int i = 0; i < img.width; i++) {
  for (int j = 0; j < img.height; j++) {
    int bright = int(brightness(get(i, j)));
    hist[bright]++; 
  }
}

```

what is the difference between that and

```auto
for (int i = 0; i < img.width; i++) {
  for (int j = 0; j < img.height; j++) {
    int bright = int(brightness(pixels[i+ j*img.width]));
    hist[bright]++; 
  }
}

```

other than the fact that pixels require loadPixels.

---

<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:** [October 10, 2020, 4:52am UTC](https://discourse.processing.org/t/difference-between-loadpixels-and-get/24491/2 "2020-10-10T04:52:57Z")

</div>

> [@paulgoux](#):
>
> What is the difference between that and…

Just have a look at **get()**: 👓

> <https://github.com/processing/processing/blob/processing-0270-3.5.4/core/src/processing/core/PImage.java#L727-L741>

As we can see for ourselves **get()** does pretty much the same as we’d do by directly accessing _pixels[]_, but w/ more checks; therefore slightly slower. 🐌

> [@paulgoux](#):
>
> … other than the fact that _pixels_ require **loadPixels()**.

Method **get()** doesn’t invoke **loadPixels()** either! 😱

So we should assume it’s needed as much as using _pixels[]_ on our own. 🤭

> [@paulgoux](#):
>
> ```auto
> for (int i = 0; i < img.width; i++) {
> for (int j = 0; j < img.height; j++) {
> 
> ```

For best contiguous memory speed access the outer loop gotta be _height_ & the inner 1 be _width_: 🏎

```auto
for (int h = img.height, y = 0; y < h; ++y)
  for (int w = img.width, x = 0; x < w; ++x) {

```
