2

The following code, which i took from the blackberry forum, takes almost 26 minutes to create 2000 random contacts using the BB PIM API on an 8500 series set. Does anyone have any idea why it takes so long or how to improve the performance of contact creation in the address book?

public static void testContactCreation() {
    ContactList contacts = null;
    try {
      contacts = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
    } catch (PIMException e) {
      // An error occurred
      return;
    }

    String arrSzCities[] = {"Birmingham", "Walsall", "Wolverhampton", "Banbury", "Bromsgrove", "Lichfield", "Balsall Heath",
        "West Bromich", "Smethwick", "Scott Arms", "Perry Barr", "Small Heath", "Acocks Green", "Great Barr",
        "Harborne", "Selly Oak", "Newtown", "Hockley", "Nuneaton", "Stafford", "Stoke", "Sandwell", "Brierly Hill",
        "Longbridge", "Sutton Coldfield", "Tamworth", "Coventry", "Rugby", "Hall Green", "Olton", "Dorridge",
        "Lapworth", "Shirley", "Wythall", "Warwick", "Dudley", "Barnt Green", "Tile Hill", "Berkswell", "Canley",
        "Yardley", "Yardley Wood", "Bordesley Green", "Cosely", "Four Oaks", "Erdington", "Aston", "Duddington"};

    String arrSzCountries[] = {"England", "Wales", "Scotland", "Northern Ireland", "Eire", "Spain", "France", "Italy",
        "Monaco", "Switzerland", "Austria", "Germany", "Lapland", "Estonia", "Hungary", "Slovakia", "Slovenia",
        "Czech Republic", "Latvia", "Holland", "Belgium", "Luxembourg", "Iceland", "Finland", "Denmark", "Norway"};

    String arrSzFamilyNames[] = {"SMITH", "JOHNSON", "WILLIAMS", "BROWN", "JONES", "MILLER", "DAVIS", "GARCIA", "RODRIGUEZ",
        "WILSON", "MARTINEZ", "ANDERSON", "TAYLOR", "THOMAS", "HERNANDEZ", "MOORE", "MARTIN", "JACKSON", "THOMPSON",
        "WHITE", "LOPEZ", "LEE", "GONZALEZ", "HARRIS", "CLARK", "LEWIS", "ROBINSON", "WALKER", "PEREZ", "HALL", "YOUNG",
        "ALLEN", "SANCHEZ", "WRIGHT", "KING", "SCOTT", "GREEN", "BAKER", "ADAMS", "NELSON", "HILL", "RAMIREZ", "CAMPBELL",
        "MITCHELL", "ROBERTS", "CARTER", "PHILLIPS", "EVANS", "TURNER", "TORRES", "PARKER", "COLLINS", "EDWARDS", "STEWART",
        "FLORES", "MORRIS", "NGUYEN", "MURPHY", "RIVERA", "COOK", "ROGERS", "MORGAN", "PETERSON", "COOPER", "REED",
        "BAILEY", "BELL", "GOMEZ", "KELLY", "HOWARD", "WARD", "COX", "DIAZ", "RICHARDSON", "WOOD", "WATSON", "BROOKS",
        "BENNETT", "GRAY", "JAMES", "REYES", "CRUZ", "HUGHES", "PRICE", "MYERS", "LONG", "FOSTER", "SANDERS", "ROSS",
        "MORALES", "POWELL", "SULLIVAN", "RUSSELL", "ORTIZ", "JENKINS", "GUTIERREZ", "PERRY", "BUTLER", "BARNES", "FISHER",
        "HENDERSON", "COLEMAN", "SIMMONS", "PATTERSON", "JORDAN", "REYNOLDS", "HAMILTON", "GRAHAM", "KIM", "GONZALES",
        "ALEXANDER", "RAMOS", "WALLACE", "GRIFFIN", "WEST",
        "COLE", "HAYES", "CHAVEZ", "GIBSON", "BRYANT", "ELLIS", "STEVENS", "MURRAY", "FORD", "MARSHALL", "OWENS",
        "MCDONALD", "HARRISON", "RUIZ", "KENNEDY", "WELLS", "ALVAREZ", "WOODS", "MENDOZA", "CASTILLO", "OLSON",
        "WEBB", "WASHINGTON", "TUCKER", "FREEMAN", "BURNS", "HENRY", "VASQUEZ", "SNYDER", "SIMPSON", "CRAWFORD", "JIMENEZ",
        "PORTER", "MASON", "SHAW", "GORDON", "WAGNER", "HUNTER", "ROMERO", "HICKS", "DIXON", "HUNT", "PALMER", "ROBERTSON",
        "BLACK", "HOLMES", "STONE", "MEYER", "BOYD", "MILLS", "WARREN", "FOX", "ROSE", "RICE", "MORENO", "SCHMIDT", "PATEL",
        "FERGUSON", "NICHOLS", "HERRERA", "MEDINA", "RYAN", "FERNANDEZ", "WEAVER", "DANIELS", "STEPHENS", "GARDNER", "PAYNE",
        "KELLEY", "DUNN", "PIERCE", "ARNOLD", "TRAN", "SPENCER", "PETERS", "HAWKINS", "GRANT", "HANSEN", "CASTRO", "HOFFMAN",
        "HART", "ELLIOTT", "CUNNINGHAM", "KNIGHT"};

    String arrSzFirstNames[] = {"MARY", "PATRICIA", "LINDA", "BARBARA", "ELIZABETH", "JENNIFER", "MARIA", "SUSAN", "MARGARET",
        "DOROTHY", "LISA", "NANCY", "KAREN", "BETTY", "HELEN", "SANDRA", "DONNA", "CAROL", "RUTH", "SHARON", "MICHELLE",
        "LAURA", "SARAH", "KIMBERLY", "DEBORAH", "JESSICA", "SHIRLEY", "CYNTHIA", "ANGELA", "MELISSA", "BRENDA", "AMY",
        "ANNA", "REBECCA", "VIRGINIA", "KATHLEEN", "PAMELA", "MARTHA", "DEBRA", "AMANDA", "STEPHANIE", "CAROLYN", "CHRISTINE",
        "MARIE", "JANET", "CATHERINE", "FRANCES", "ANN", "JOYCE", "DIANE", "ALICE", "JULIE", "HEATHER", "TERESA", "DORIS",
        "GLORIA", "EVELYN", "JEAN", "CHERYL", "MILDRED", "GERALD", "KEITH", "SAMUEL",
        "JAMES", "JOHN", "ROBERT", "MICHAEL", "WILLIAM", "DAVID", "RICHARD", "CHARLES", "JOSEPH", "THOMAS", "CHRISTOPHER",
        "DANIEL", "PAUL", "MARK", "DONALD", "GEORGE", "KENNETH", "STEVEN", "EDWARD", "BRIAN", "RONALD", "ANTHONY",
        "KEVIN", "JASON", "MATTHEW", "GARY", "TIMOTHY", "JOSE", "LARRY", "JEFFREY", "FRANK", "SCOTT", "ERIC", "STEPHEN",
        "ANDREW", "RAYMOND", "GREGORY", "JOSHUA", "JERRY", "DENNIS", "WALTER", "PATRICK", "PETER", "HAROLD", "DOUGLAS",
        "HENRY", "CARL", "ARTHUR", "RYAN", "ROGER", "JOE", "JUAN", "JACK", "ALBERT", "JONATHAN", "JUSTIN", "TERRY"};

    String arrSzEmailProviders[] = {"google", "yahoo", "wanadoo", "freemail", "aol", "yellowmellow", "redmail", "bt", "tiscali",
        "naims", "bulldog", "demon", "virgin", "sky", "orange", "vodaphone", "o2", "three", "britishgas", "npower",
        "britishtelecom", "royalmail", "parcelforce", "dhl", "usps", "ford", "rover", "fiat", "seat", "volvo", "bmw",
        "landrover", "jaguar", "warburtons", "kingsmill", "hovis", "walkers", "cadburys", "ironbru", "redbull", "jura"};

    String arrSzAlphas[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
        "U", "V", "W", "X", "Y", "Z"};

    String arrSzStreetSuffixes[] = {"Road", "Street", "Crescent", "Close", "Way", "Mews", "Common", "Alley", "Common", "Grove",
        "Place", "Mill", "Manor", "Lane", "March", "Hill", "Park", "Passage", "Path", "Row", "Square", "Terrace", "View"};

    String arrSzStreetNames[] = {"High", "Station", "Main", "Park", "Church", "London", "Victoria", "Albert", "Green", "Manor",
        "Church", "Park", "Queens", "New", "Grange Road", "Kings Road", "North", "West", "South", "East", "Windsor",
        "Highfield", "Mill", "Alexander", "York", "St. John's", "Broad", "Springfield", "George", "Manchester", "Richmond",
        "School", "Stanley", "Chester", "Aghaloo", "Picadilly", "**bleep**", "Wooburn", "Crazies", "Valentia", "Luton", "Croydon",
        "Rookery", "Coronation", "Dawlish", "Tiverton", "Dartmouth", "Hubert", "Bristol", "Arley", "Grange", "Dale",
        "Serpentine", "Bournebrook", "University", "Holly", "Kitchener", "Millner", "Westminster", "Cherrington", "Gristhorpe",
        "Kensington", "Cartland", "Horatio", "Ethelbert", "Hornblower", "Hanky Panky", "Bewdley", "Acorn", "Berry", "Moor",
        "Brent", "Mungo Jerry", "Highbury", "Howell", "Tenbury", "Peacock", "Hartswell"};

    Random generator = new Random(1628434416);
    for (int i = 0; i < 2000; i++) {
      Contact contact = contacts.createContact();
      String[] addr = new String[contacts.stringArraySize(Contact.ADDR)];
      String[] szName = new String[contacts.stringArraySize(Contact.NAME)];

      // Family Name
      if (contacts.isSupportedArrayElement(Contact.NAME, Contact.NAME_FAMILY))
        szName[Contact.NAME_FAMILY] = arrSzFamilyNames[generator.nextInt(arrSzFamilyNames.length)];
      // First Name
      if (contacts.isSupportedArrayElement(Contact.NAME, Contact.NAME_GIVEN))
        szName[Contact.NAME_GIVEN] = arrSzFirstNames[generator.nextInt(arrSzFirstNames.length)];
      // Add name to contact
      contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, szName);

      // Postcode (UK format)
      if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_POSTALCODE))
        addr[Contact.ADDR_POSTALCODE] =
            arrSzAlphas[generator.nextInt(arrSzAlphas.length)] + arrSzAlphas[generator.nextInt(arrSzAlphas.length)]
                + Integer.toString(generator.nextInt(99)) + Integer.toString(generator.nextInt(9)) + " "
                + arrSzAlphas[generator.nextInt(arrSzAlphas.length)] + arrSzAlphas[generator.nextInt(arrSzAlphas.length)];

      // Street and number
      if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_STREET))
        addr[Contact.ADDR_STREET] = Integer.toString(generator.nextInt(999)) + " "
            + arrSzStreetNames[generator.nextInt(arrSzStreetNames.length)]
            + " " + arrSzStreetSuffixes[generator.nextInt(arrSzStreetSuffixes.length)];

      // Locality
      if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_LOCALITY))
        addr[Contact.ADDR_LOCALITY] = arrSzCities[generator.nextInt(arrSzCities.length)];
      // Country
      if (contacts.isSupportedArrayElement(Contact.ADDR, Contact.ADDR_COUNTRY))
        addr[Contact.ADDR_COUNTRY] = arrSzCountries[generator.nextInt(arrSzCountries.length)];
      if (contacts.isSupportedField(Contact.ADDR))
        contact.addStringArray(Contact.ADDR, Contact.ATTR_HOME, addr);

      // Email address
      if (contacts.isSupportedField(Contact.EMAIL)) {
        contact.addString(Contact.EMAIL, Contact.ATTR_HOME | Contact.ATTR_PREFERRED, szName[Contact.NAME_GIVEN] + "." + szName[Contact.NAME_FAMILY] + "@" + arrSzEmailProviders[generator.nextInt(arrSzEmailProviders.length)] + ".com");
      }

      // Telephone numbers (work and home)
      String szTelHome = "";
      String szTelWork = "";
      for (int j = 0; j < 7; j++) {
        String szDigit = Integer.toString(generator.nextInt(9));
        szTelHome = szTelHome + szDigit;
        szTelWork = szDigit + szTelWork;
      }
      if (contacts.isSupportedField(Contact.TEL)) {
        contact.addString(Contact.TEL, Contact.ATTR_HOME, szTelHome);
      }
      if (contacts.isSupportedField(Contact.TEL)) {
        contact.addString(Contact.TEL, Contact.ATTR_WORK, szTelHome);
      }

      try {
        contact.commit();
      } catch (PIMException e) {
        // An error occured

      }
    }

    try {
      contacts.close();
    } catch (PIMException e) {
    }
  }
Yasser
  • 1,808
  • 12
  • 18
  • Why would you want to create 2000 contacts in the first place ? – Michael B. May 27 '11 at 13:44
  • I think that is irrelevant to my question. BTW, i have at least 2000 contacts in my address book so i dont think its unusual or uncommon. – Yasser May 27 '11 at 19:18
  • Are you sure it's the contact adding that's taking a long time and not the random contact generation? – jprofitt May 31 '11 at 13:56
  • Yes, pretty certain but i would like to be proven wrong :) – Yasser May 31 '11 at 17:37
  • Maybe try commenting out all of the logic in the loop, and just have it print strings with the details of what you would be adding. If that takes a long time you'll know where the culprit is! – jprofitt Jun 01 '11 at 13:20
  • I can confirm that the commit() is taking most of execution time. I created a vector to save the contracts in and moved the committing out of the main loop and into it's own loop. The loop which creates the contracts takes just a couple of seconds, the loop which calls commit on the contracts in the vector takes 20+ mins. I'm trying to research if there is a way to do a batch commit, like you can with the PersistentStore. – eSniff Jun 04 '11 at 17:31
  • I never did find a way to do batch committing with a PIM object. Maybe it would be faster to write this PIMList to a FileStream and reading it from there. – eSniff Jun 07 '11 at 17:44

1 Answers1

2

Hm.. 26 mins for 2000 contacts means 780 ms per one contact.

1). Could you check the contact.commit(); is not the most time consuming call here? You could do this comparatively accurate with smth like this:

long timeSpentOnCommits; // defined somewhere at a top level
...
long start = System.currentTimeMillis();
contact.commit();
long taken = System.currentTimeMillis() - start;
timeSpentOnCommits += taken;
...
// then after the all contacts have been added you can get 
// an average commit time:
long timePerOneCommit = timeSpentOnCommits / 2000;
// view the got value in a `Dialog` or some logging

If it takes most of the time, then I don't think you can improve it.

2). Another idea - in the 'for' loop you're creating too many String objects, so this forces the OS to call garbage collection too frequently (which is a time consuming thing).

Use StringBuffer instead. So instead of the

addr[Contact.ADDR_POSTALCODE] =
    arrSzAlphas[generator.nextInt(arrSzAlphas.length)]
    + arrSzAlphas[generator.nextInt(arrSzAlphas.length)]
    + Integer.toString(generator.nextInt(99)) 
    + Integer.toString(generator.nextInt(9)) 
    + " "
    + arrSzAlphas[generator.nextInt(arrSzAlphas.length)]
    + arrSzAlphas[generator.nextInt(arrSzAlphas.length)];

use this:

addr[Contact.ADDR_POSTALCODE] = new StringBuffer()
    .append(arrSzAlphas[generator.nextInt(arrSzAlphas.length)])
    .append(arrSzAlphas[generator.nextInt(arrSzAlphas.length)])
    .append(generator.nextInt(99))
    .append(generator.nextInt(9))
    .append(' ')
    .append(arrSzAlphas[generator.nextInt(arrSzAlphas.length)])
    .append(arrSzAlphas[generator.nextInt(arrSzAlphas.length)])
    .toString();

Note that StringBuffer.append() is overloaded for any primitive type, so it can accept ints and chars directly without the need to be converted to String objects.

The same approach should be used for telephone numbers generation.

3). Minor speed improvements could be got via extracting all repeating calls from the 'for' loop. For instance, just count how much times you call for arrSzAlphas.length (2000 iterations * 4 calls per iteration = 8000 times!), while you could just call it ONCE before the loop and store it in a final local variable visible from the loop. The same is for a set of contacts.isSupportedXXXX calls - you should not call it over and over in all iterations, just call it once before the loop starts.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
  • When i asked the question, i knew that the commit() call was taking the bulk of time so i was asking for explanations of why that is and if that can be improved. All other improvements are minor or insignificant. – Yasser Jun 07 '11 at 06:46
  • @Yasser. OK, I think you should have stated that in a more explicit way narrowing down to the `contact.commit()` call. – Vit Khudenko Jun 07 '11 at 07:20
  • Yes, sorry about that. I am obviously interested in bringing the time down from 26 minutes to 5 minutes, not from 26 minutes to 25 minutes and 58 seconds :) – Yasser Jun 07 '11 at 09:37