Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
The reference code used in running performance metrics for loops
public with sharing class PreferencesManager {
//public static Account setEmailPreferences(Account acc, Boolean preference) {
// for(Integer i = 0; i < acc.Contacts.size(); i++) {
// acc.Contacts[i].Email_Preference__c = preference;
// }
// return acc;
//}
//2374
//public static Account setEmailPreferences(Account acc, Boolean preference) {
// for(Integer i = 0, j = acc.Contacts.size(); i < j; i++) {
// acc.Contacts[i].Email_Preference__c = preference;
// }
// return acc;
//}
////1350
//public static List<Contact> setEmailPreferences(Account acc, Boolean preference) {
// List<Contact> contacts = acc.Contacts;
// for(Integer i = 0, j = contacts.size(); i < j; i++) {
// contacts[i].Email_Preference__c = preference;
// }
// return contacts;
//}
//210
//Steven Herod - @sherod
public static List<Contact> setEmailPreferences(Account acc, Boolean preference) {
List<Contact> contacts = acc.Contacts;
for(Contact con : contacts) {
con.Email_Preference__c = preference;
}
return contacts;
}
//Andy Mahood - @andymahood__c (Query timed at 84 ms)
//public static void setEmailPreferences(Boolean preference) {
// for(Contact con : [SELECT Id, Email_Preference__c FROM Contact WHERE Account.Name = 'sForce']) {
// con.Email_Preference__c = preference;
// }
//}
//Andrew Fawcett - @andyinthecloud
//public static List<Contact> setEmailPreferences(Account acc, Boolean preference) {
// ContactIterator iterator = new ContactIterator(acc.Contacts);
// while(iterator.hasNext()) {
// iterator.next().Email_Preference__c = preference;
// }
// return iterator.contacts;
//}
public with sharing class ContactIterator implements Iterator<Contact> {
public List<Contact> contacts;
private Integer contactsSize;
private Integer currentIndex = 0;
public ContactIterator(List<Contact> cons) {
contacts = cons;
contactsSize = contacts.size();
}
public Boolean hasNext() {
return currentIndex < contactsSize;
}
public Contact next() {
Contact currentCon = contacts[currentIndex];
currentIndex++;
return currentCon;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment