好的,这更像是速度问题。问题是我从各种来源获取三个字段,如Contacts,Facebook和Google plus。这三个字段是日期,名称和联系人图像URi。逻辑是这样的,每个源写入文件,联系人 - 文件,Facebook - 附加到同一文件,谷歌加 - 附加到同一文件。此操作非常快,需要执行正常时间。这将创建我的原始数据源。然后,从该文件中,我分别获取每个记录(带有三个字段的行)并进行一些计算,并将与每个记录有关的数据写入三个不同的数据库表。虽然这在四核手机上运行速度非常快,但在双核手机上这个过程太慢了。处理双核上的150条记录大约需要十分钟,处理四核处理150条记录需要四分钟。我对如何提高速度感到困惑?我应该放弃将文件作为原始数据源而转而使用数据库表的想法吗?什么是最快的?
代码如下:
private class MagicCall extends AsyncTask<Void, String, String> {
int years;
long secon;
long min;
int hours;
int mon;
int days;
int weeks;
String CONTACT_ID,CONTACT_NAME,CONTACT_IMAGE_URI;
ProgressDialog Asycdialog = new ProgressDialog(LoaderClass.this);
@Override
protected void onPreExecute() {
boolean DIALOG_SHOW = true;
try{
BufferedReader br = null;
if(FLAG ==1){
br = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/fileone.txt"));
}else{
br = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/output.txt"));
}
if (br.readLine() == null) {
Toast.makeText(getApplicationContext(),"There are no contacts with Birthday details, try syncing with Facebook and/or Google", Toast.LENGTH_LONG).show();
DIALOG_SHOW = false;
}
}catch(Exception e){
System.out.println(e);
}
if(DIALOG_SHOW){
//Init the LoaderDialog
Asycdialog.setMessage("Working");
Asycdialog.getWindow().setGravity(Gravity.CENTER_VERTICAL);
Asycdialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
Asycdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Asycdialog.setCancelable(false);
//Dialog Show
Asycdialog.show();
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor = prefs.edit();
editor.putBoolean("async", true);
editor.commit();
super.onPreExecute();
}
protected void onPostExecute(String result) {
// hide the dialog
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, h:mm a");
String date = df.format(Calendar.getInstance().getTime());
SharedPreferences prefsx = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editorx = prefsx.edit();
editorx.putString("refresh", date);
editorx.commit();
dbHelper.close();
Asycdialog.dismiss();
startActivity(new Intent(LoaderClass.this, MainActivity.class));
finish();
super.onPostExecute(result);
}
@Override
protected String doInBackground(Void... args) {
dbHelper = new DBAdapter(getApplicationContext());
dbHelper.open();
int i=0;
int lines = 0;
// int prog = fetchAdhoc();
String progress = null;
String dateToInsert;
try{
BufferedReader reader;
if(FLAG ==1){
reader = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/fileone.txt"));
}else{
reader = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/xxxinders/output.txt"));
}
while (reader.readLine() != null) lines++;
reader.close();
}catch(Exception e){
}
Cursor c = dbHelper.fetchAdhoc();
ADHOC_COUNT = (c.getCount());
lines=lines+ADHOC_COUNT;
dbHelper.NotificationDrop();
if (c.moveToFirst()) {
do {
try{
String name = c.getString(c.getColumnIndexOrThrow("Name"));
String displayBirthday = c.getString(c.getColumnIndexOrThrow("DOB"));
String imageData = c.getString(c.getColumnIndexOrThrow("ImageData"));
String primaryAdhoc = c.getString(c.getColumnIndexOrThrow("_id"));
//CONTACT_ID,CONTACT_NAME,CONTACT_IMAGE_URI
CONTACT_ID = primaryAdhoc;
CONTACT_NAME = toTitleCase(name);
CONTACT_IMAGE_URI = imageData;
dateToInsert = displayBirthday;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(dateToInsert);
java.sql.Date dx = new java.sql.Date(date.getTime());
Date key = dx;
years = getDiffYear(key); // For years elapsed
secon = seconds(key); // for seconds elapsed
min = seconds(key) / 60; // For minutes elapsed
hours = (int) (seconds(key) / 60) / 60; // For hours elapsed
mon = months(String.valueOf(key)); // for months elapsed
days = daysElapsed(key); // Days elapsed
weeks = daysElapsed(key) / 7; // For weeks
System.out.println(lines);
progress = ("" + (CONTACT_NAME) + "\n"+ i + " of " + lines + " Contacts");
dbHelper.insert(dateToInsert, CONTACT_NAME, String.valueOf(days), String.valueOf(hours), CONTACT_IMAGE_URI, String.valueOf(min),String.valueOf(mon), String.valueOf(secon), CONTACT_ID, String.valueOf(weeks), String.valueOf(years));
int PRIMARY_ID = dbHelper.getPrimaryId(); // Fetch the PrimaryId (_id) of the above inserted row, its the Foreign key for Notification and SpecialNotifications Table.
String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID); // Same as above, but fetches the Name field of the last inserted row.
//=========================================================================
//**Database Insertions Notifications Table/ SpecialNotifications Table**
//=========================================================================
//=======================================================================================//
//Regular intervals DB Insertions:
//======================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//======================================================================================//
//==============================
//For Months
//==============================
intCal.monthsNotify(mon, dateToInsert);
int monSpecial = intCal.getMonthRegular();
Date dateMonReg = intCal.getMonRegDate();
dbHelper.insertNotifications(1, convertDate(dateMonReg), 0, monSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Weeks
//===============================
intCal.weeksToNotify(weeks,dateToInsert);
int weekSpecial = intCal.getWeekRegular();
Date dateWeekReg =intCal.getWeekRegDate();
dbHelper.insertNotifications(2, convertDate(dateWeekReg), 0, weekSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Days
//===============================
intCal.daysToNotify(days, dateToInsert);
int daysSpecial= intCal.getDaysRegular();
Date dateDaysReg = intCal.getDaysRegDate();
dbHelper.insertNotifications(3, convertDate(dateDaysReg), 0, daysSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For minutes
//===============================
intCal.minutesToNotify(min,dateToInsert);
long minutesSpecial= intCal.getMinutesRegular();
Date dateMinsReg = intCal.getMinutesRegDate();
dbHelper.insertNotifications(4, convertDate(dateMinsReg), 0,(int) minutesSpecial,FOREIGN_KEY,PRIMARY_ID);
//==============================
//For Years
//==============================
intCal.yearsToNotify(years, dateToInsert);
int yearsSpecial = intCal.getYearsRegular();
Date dateYearsReg = intCal.getYearsRegDate();
dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Seconds
//=============================
intCal.secondsToNotify(secon, dateToInsert);
long secondsSpecial= intCal.getSecondsRegular();
Date dateSecondsReg = intCal.getSecondsRegDate();
dbHelper.insertNotifications(6, convertDate(dateSecondsReg), 0, secondsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Hours
//=============================
intCal.hoursToNotify(hours, dateToInsert);
int hoursSpecial= intCal.getHoursRegular();
Date dateHoursReg= intCal.getHoursRegDate();
dbHelper.insertNotifications(7, convertDate(dateHoursReg), 0, hoursSpecial,FOREIGN_KEY,PRIMARY_ID);
//============================================================================================//
//Special Intervals
//============================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//For Years
intCal.specialIntervalYears(years, dateToInsert);
int yearsOnceSpecial =intCal.getYearsSpecial();
Date dateYearsSpecial = intCal.getYearsSpDate();
dbHelper.insertSpecialNotifications(5, convertDate(dateYearsSpecial), yearsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Months
intCal.specialIntervalMonths(mon,dateToInsert);
int monthsOnceSpecial= intCal.getMonthsSpecial();
Date dateMonthsSpecial = intCal.getMonthsSpDate();
dbHelper.insertSpecialNotifications(1, convertDate(dateMonthsSpecial), monthsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Weeks
intCal.specialIntervalsWeeks(weeks,dateToInsert);
int weeksOnceSpecial= intCal.getWeeksSpecial();
Date dateWeeksSpecial = intCal.getWeeksSpDate();
dbHelper.insertSpecialNotifications(2, convertDate(dateWeeksSpecial), weeksOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Days
intCal.specialIntervalsDays(days, dateToInsert);
int daysOnceSpecial= intCal.getDaysSpecial();
Date dateDaysSpecial = intCal.getDaysSpDate();
dbHelper.insertSpecialNotifications(3, convertDate(dateDaysSpecial), daysOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Hours
intCal.specialIntervalsHours(hours,dateToInsert);
int hoursOnceSpecial= intCal.getHoursSpecial();
Date dateHoursSpecial = intCal.getHoursSpDate();
dbHelper.insertSpecialNotifications(7, convertDate(dateHoursSpecial), hoursOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Minutes
intCal.specialIntervalMinutes(min,dateToInsert);
long minutesOnceSpecial= intCal.getMinutesSpecial();
Date dateMinutesSpecial= intCal.getMinutesSpDate();
dbHelper.insertSpecialNotifications(4, convertDate(dateMinutesSpecial), (int)minutesOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Seconds
intCal.specialIntervalsSeconds(secon,dateToInsert);
long secondsOnceSpecial= intCal.getSecondsSpecial();
Date dateSecondsSpecial= intCal.getSecondsSpDate();
dbHelper.insertSpecialNotifications(6, convertDate(dateSecondsSpecial), secondsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
i++;
}catch(Exception e){
}
publishProgress(progress);
Asycdialog.setMax(lines);
Asycdialog.incrementProgressBy(1);
}while(c.moveToNext());
}
//
// Dear maintainer:
//
// Once you are done trying to 'optimise' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
//
// total_hours_wasted_here = 0;
//
//int i=0;
File toRead = null;
try{
if(FLAG ==1){
toRead=new File(getApplicationContext().getFilesDir()+"/xxxinders/fileone.txt");
}else{
toRead=new File(getApplicationContext().getFilesDir()+"/xxxinders/output.txt");
}
FileInputStream fis=new FileInputStream(toRead);
Scanner sc=new Scanner(fis);
String currentLine;
while(sc.hasNextLine()){
currentLine=sc.nextLine();
StringTokenizer st=new StringTokenizer(currentLine,"=",false);
CONTACT_NAME = toTitleCase(st.nextToken());
if(CONTACT_NAME.contains("'")){
CONTACT_NAME = CONTACT_NAME.replace("'", "");
}
// *********
String listStr = st.nextToken();
String cut = listStr.substring(1, listStr.length() - 1);
String[] array = cut.split(",");
CONTACT_ID = (array[0].trim());
String dateStr = (array[1].trim());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(dateStr);
java.sql.Date dx = new java.sql.Date(date.getTime());
Date key = dx;
dateToInsert = String.valueOf(dx);
CONTACT_IMAGE_URI = (array[2].trim());
if (isCancelled()) {
break;
}
progress = ("" + Character.toUpperCase(CONTACT_NAME.charAt(0)) + CONTACT_NAME.substring(1) + "\n"+i + " of " + lines + " Contacts"); // Progress displayed here.
years = getDiffYear(key); // For years elapsed
secon = seconds(key); // for seconds elapsed
min = seconds(key) / 60; // For minutes elapsed
hours = (int) (seconds(key) / 60) / 60; // For hours elapsed
mon = months(String.valueOf(key)); // for months elapsed
days = daysElapsed(key); // Days elapsed
weeks = daysElapsed(key) / 7; // For weeks
//===============================================================================================================
if (dateToInsert.contains("0001-") == true){ //Special Case, we added 0001 to Birthdays Which Have NO Year field.
//===========================================================================================================
dbHelper.insert(dateToInsert, CONTACT_NAME, "","", CONTACT_IMAGE_URI, "", "", "", CONTACT_ID, "", ""); // All other fields will be empty, because we don't have a Year.
int PRIMARY_ID = dbHelper.getPrimaryId();
String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID);
//=====================================================================================================
//In this case we are only interested in fetching the year alert for next birthday of this contact -->
//=====================================================================================================
intCal.yearsToNotify(years, dateToInsert);
int yearsSpecial = intCal.getYearsRegular();
Date dateYearsReg = intCal.getYearsRegDate();
dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);
}
//=========================================================================
//Case when all the Date fields exist and we set up notifications --->
//=========================================================================
else if(dateToInsert != "null" && dateToInsert.contains("0001-") != true){
dbHelper.insert(dateToInsert, CONTACT_NAME, String.valueOf(days), String.valueOf(hours), CONTACT_IMAGE_URI, String.valueOf(min),String.valueOf(mon), String.valueOf(secon), CONTACT_ID, String.valueOf(weeks), String.valueOf(years));
int PRIMARY_ID = dbHelper.getPrimaryId(); // Fetch the PrimaryId (_id) of the above inserted row, its the Foreign key for Notification and SpecialNotifications Table.
String FOREIGN_KEY = dbHelper.getHighestID(PRIMARY_ID); // Same as above, but fetches the Name field of the last inserted row.
//=========================================================================
//**Database Insertions Notifications Table/ SpecialNotifications Table**
//=========================================================================
//=======================================================================================//
//Regular intervals DB Insertions:
//======================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//======================================================================================//
//==============================
//For Months
//==============================
intCal.monthsNotify(mon, dateToInsert);
int monSpecial = intCal.getMonthRegular();
Date dateMonReg = intCal.getMonRegDate();
dbHelper.insertNotifications(1, convertDate(dateMonReg), 0, monSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Weeks
//===============================
intCal.weeksToNotify(weeks,dateToInsert);
int weekSpecial = intCal.getWeekRegular();
Date dateWeekReg =intCal.getWeekRegDate();
dbHelper.insertNotifications(2, convertDate(dateWeekReg), 0, weekSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For Days
//===============================
intCal.daysToNotify(days, dateToInsert);
int daysSpecial= intCal.getDaysRegular();
Date dateDaysReg = intCal.getDaysRegDate();
dbHelper.insertNotifications(3, convertDate(dateDaysReg), 0, daysSpecial,FOREIGN_KEY,PRIMARY_ID);
//===============================
//For minutes
//===============================
intCal.minutesToNotify(min,dateToInsert);
long minutesSpecial= intCal.getMinutesRegular();
Date dateMinsReg = intCal.getMinutesRegDate();
dbHelper.insertNotifications(4, convertDate(dateMinsReg), 0,(int) minutesSpecial,FOREIGN_KEY,PRIMARY_ID);
//==============================
//For Years
//==============================
intCal.yearsToNotify(years, dateToInsert);
int yearsSpecial = intCal.getYearsRegular();
Date dateYearsReg = intCal.getYearsRegDate();
dbHelper.insertNotifications(5, convertDate(dateYearsReg), 0, yearsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Seconds
//=============================
intCal.secondsToNotify(secon, dateToInsert);
long secondsSpecial= intCal.getSecondsRegular();
Date dateSecondsReg = intCal.getSecondsRegDate();
dbHelper.insertNotifications(6, convertDate(dateSecondsReg), 0, secondsSpecial,FOREIGN_KEY,PRIMARY_ID);
//=============================
//For Hours
//=============================
intCal.hoursToNotify(hours, dateToInsert);
int hoursSpecial= intCal.getHoursRegular();
Date dateHoursReg= intCal.getHoursRegDate();
dbHelper.insertNotifications(7, convertDate(dateHoursReg), 0, hoursSpecial,FOREIGN_KEY,PRIMARY_ID);
//============================================================================================//
//Special Intervals
//============================================================================================//
//Notification Types:
//1 for months
//2 for weeks
//3 for days
//4 for minutes
//5 for years
//6 for seconds
//7 for hours
//For Years
intCal.specialIntervalYears(years, dateToInsert);
int yearsOnceSpecial =intCal.getYearsSpecial();
Date dateYearsSpecial = intCal.getYearsSpDate();
dbHelper.insertSpecialNotifications(5, convertDate(dateYearsSpecial), yearsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Months
intCal.specialIntervalMonths(mon,dateToInsert);
int monthsOnceSpecial= intCal.getMonthsSpecial();
Date dateMonthsSpecial = intCal.getMonthsSpDate();
dbHelper.insertSpecialNotifications(1, convertDate(dateMonthsSpecial), monthsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Weeks
intCal.specialIntervalsWeeks(weeks,dateToInsert);
int weeksOnceSpecial= intCal.getWeeksSpecial();
Date dateWeeksSpecial = intCal.getWeeksSpDate();
dbHelper.insertSpecialNotifications(2, convertDate(dateWeeksSpecial), weeksOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Days
intCal.specialIntervalsDays(days, dateToInsert);
int daysOnceSpecial= intCal.getDaysSpecial();
Date dateDaysSpecial = intCal.getDaysSpDate();
dbHelper.insertSpecialNotifications(3, convertDate(dateDaysSpecial), daysOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Hours
intCal.specialIntervalsHours(hours,dateToInsert);
int hoursOnceSpecial= intCal.getHoursSpecial();
Date dateHoursSpecial = intCal.getHoursSpDate();
dbHelper.insertSpecialNotifications(7, convertDate(dateHoursSpecial), hoursOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Minutes
intCal.specialIntervalMinutes(min,dateToInsert);
long minutesOnceSpecial= intCal.getMinutesSpecial();
Date dateMinutesSpecial= intCal.getMinutesSpDate();
dbHelper.insertSpecialNotifications(4, convertDate(dateMinutesSpecial), (int)minutesOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
//For Seconds
intCal.specialIntervalsSeconds(secon,dateToInsert);
long secondsOnceSpecial= intCal.getSecondsSpecial();
Date dateSecondsSpecial= intCal.getSecondsSpDate();
dbHelper.insertSpecialNotifications(6, convertDate(dateSecondsSpecial), secondsOnceSpecial,FOREIGN_KEY,PRIMARY_ID);
writeToSD();
}
publishProgress(progress);
Asycdialog.setMax(lines);
Asycdialog.incrementProgressBy(1);
i++;
}
writeToSD();
}catch (Exception e){
System.out.println(e);
} finally{
dbHelper.close();
}
return "";
}
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Asycdialog.setMessage("" + values[0]);
}
}