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

发布于 2024-11-09 19:02:12 字数 9974 浏览 4 评论 0原文

以下代码是我从黑莓论坛获取的,在 8500 系列设备上使用 BB PIM API 花费了近 26 分钟来创建 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) {
    }
  }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2024-11-16 19:02:12

嗯.. 26 分钟 2000 次接触意味着每次接触 780 毫秒。

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 次!),而您可以在循环之前调用一次并将其存储在从循环中可见的最终局部变量。对于一组 contacts.isSupportedXXXX 调用也是如此 - 您不应在所有迭代中一遍又一遍地调用它,只需在循环开始之前调用一次即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文