question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Iterating result very slow

See original GitHub issue

Hi,

Here are my java classes :

public class PublicArticleEntity extends RealmObject {

    /**
     * For fast import no @PrimaryKey defined
     */
    private String id;

    private String type;

    @Index
    private String manufacturerId;

    private Boolean sterilizable;

    private Boolean active;

    private Date importModified;

    private RealmList<PublicArticlePartNumberEntity> partNumbers = new RealmList<>();
    private RealmList<PublicArticleCodificationEntity> codifications = new RealmList<>();

public class PublicArticlePartNumberEntity extends RealmObject {

    @Index
    private String partNumber;

    @Index
    private String normalizedPartNumber;

    private PublicArticleEntity publicArticle;
}

public class PublicArticleCodificationEntity extends RealmObject {

    @Index
    private String type;

    @Index
    private String value;

    private PublicArticleEntity publicArticle;
}

And here is the piece of code where I have a problem. The query returns 24 results and I use the limit to get the 21 first elements. It tooks 2000ms to do the iteration 😮

RealmResults<PublicArticlePartNumberEntity> entities = realm.where(PublicArticlePartNumberEntity.class)
                .equalTo("publicArticle.manufacturerId", manufacturerId.toString())
                .beginGroup()
                .contains("partNumber", search, Case.INSENSITIVE)
                .or()
                .contains("normalizedPartNumber", search, Case.INSENSITIVE)
                .endGroup()
                .findAllSorted("partNumber", Sort.ASCENDING);

        Set<String> items = new HashSet<>();
        int listSize = entities.size();
        for (int i = 0; i < listSize && i < limit; i++) {
            items.add(entities.get(i).getPartNumber());
        }

Here the state of each : screenshot at oct 06 15-04-13

Because Realm is using lazy loading the request if very very fast, but the for is taking 2s. Did I miss something ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:31 (25 by maintainers)

github_iconTop GitHub Comments

3reactions
mgohincommented, Oct 11, 2017

So here is the test as @beeender requested. I’m in debug mode so it takes more time and on a production device that is waaaaaay slower than my One Plus 3T, but it’s a good thing to see differences.

First test with current code (not changed) : first

Test with code

entities.get(0).getPartNumber();
int listSize = entities.size();
screenshot at oct 11 12-38-29
0reactions
mgohincommented, Oct 16, 2017

If I use contains it’s a bit long for UX. I’ll stick with beginWith for now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Iterating through a list is very slow, how to improve the ...
This is a bad way to sort. You're effectively looping over the list 4 times, extracting specific groups of items each time.
Read more >
Performance issue iterating FeatureQueryResult
I have a VB.net application that is using QueryFeaturesAsync to retrieve a FeatureQueryResult from a shapefile. It typically returns between ...
Read more >
Very slow loop - Performance - Julia Programming Language
Hi all, I am trying to speed up this loop. ... The time goes to 0.2 seconds here if I comment that, and...
Read more >
Slow performance when iterating over large documents takes ...
When iterating over queried cursor, getting each document takes up to 0.5s. Is that normal performance for larger files or is there ...
Read more >
Why Your Loops Are So Slow | by Emmett Boudreau
Another one is O(log n), which is where there is slightly more tax, but with more iterations the loop plateaus. This is popular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found