Blackberry PIM为什么联系人创建这么慢?

时间:2011-05-27 12:07:10

标签: blackberry contacts addressbook blackberry-jde jde

我从黑莓论坛上获取的以下代码需要近26分钟才能在8500系列上使用BB PIM API创建2000个随机联系人。有没有人知道为什么需要这么长时间或如何提高地址簿中联系人创建的性能?

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) {
    }
  }

1 个答案:

答案 0 :(得分:2)

Hm .. 2000接触26分钟意味着每个接触780 ms。

1)。你能检查contact.commit();这里不是最费时的电话吗?你可以用这样的方式比较准确地做到这一点:

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

如果花费大部分时间,那么我认为你不能改进它。

2)。另一个想法 - 在'for'循环中你创建了太多String个对象,所以这迫使操作系统过于频繁地调用垃圾收集(这是一件很费时间的事情)。

请改用StringBuffer。而不是

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)];

使用它:

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();

请注意,StringBuffer.append()会因任何基本类型而重载,因此它可以直接接受整数和字符,而无需转换为String个对象。

应该使用相同的方法来生成电话号码。

3)。通过从'for'循环中提取所有重复调用,可以获得较小的速度提升。例如,只计算你调用arrSzAlphas.length的次数(2000次迭代*每次迭代4次调用= 8000次!),而你可以在循环之前将其称为ONCE并将其存储在最终的局部变量中。循环。对于一组contacts.isSupportedXXXX调用也是如此 - 您不应该在所有迭代中反复调用它,只需在循环开始之前调用它一次。