void test_hardest(void) { std::cout << "test: add all words, get a random nth popular word, increase that " << "words count by 1 then get the most common word along with its count" << std::endl; std::vector<std::string> words = getWordList("data/28885.txt"); std::random_device rd; std::default_random_engine rand(rd()); TRENDS_CLASS tr; for (unsigned int i = 0; i < words.size(); i++){ tr.increaseCount(words[i], 1); } double start = getTimeInMillis(); for (unsigned int i = 0; i < words.size(); i++) { unsigned int temp = rand() % tr.numEntries(); std::string word = tr.getNthPopular(temp); tr.increaseCount(word, 1); word = tr.getNthPopular(0); temp = tr.getCount(word); } double end = getTimeInMillis(); std::cout << "time per item: " << (end - start) / words.size() << " ms " << std::endl << "total time: " << (end - start) << " ms" << std::endl << std::endl; }
void test_add_all_increment_get_pop(void) { std::cout << "test: add all words then increment every word by 1 then get the most " << "popular word" << std::endl; std::vector<std::string> words = getWordList("data/28885.txt"); std::ofstream out("data/28885.txt.out" + OUT_FILE); TRENDS_CLASS tr; for (unsigned int i = 0; i < words.size(); i++){ tr.increaseCount(words[i], 1); } double start = getTimeInMillis(); for (unsigned int i = 0; i < words.size(); i++) { tr.increaseCount(words[i], 1); std::string pop = tr.getNthPopular(0); out << pop << "\n"; } double end = getTimeInMillis(); out.close(); std::cout << "time per item: " << (end - start) / words.size() << " ms " << std::endl << "total time: " << (end - start) << " ms" << std::endl << std::endl; }
void test_add_all_get_count() { std::cout << "test: add all words then get count of random words" << std::endl; std::vector<std::string> words = getWordList("data/28885.txt"); std::ofstream out("data/28885.txt.out" + OUT_FILE); std::random_device rd; std::default_random_engine rand(rd()); TRENDS_CLASS tr; for (unsigned int i = 0; i < words.size(); i++){ tr.increaseCount(words[i], 1); } double start = getTimeInMillis(); for (unsigned int i = 0; i < words.size(); i++) { std::string w = words[rand() % words.size()]; unsigned int temp = tr.getCount(w); out << w << " " << temp << "\n"; } double end = getTimeInMillis(); out.close(); std::cout << "time per item: " << (end - start) / words.size() << " ms " << std::endl << "total time: " << (end - start) << " ms" << std::endl << std::endl; }
void test_print_words_in_order_with_count(void) { std::cout << "test: add all words then print them in order with word count" << std::endl; std::vector<std::string> words = getWordList("data/28885.txt"); std::ofstream out("data/28885.txt.out" + OUT_FILE); TRENDS_CLASS tr; for(unsigned int i = 0; i < words.size(); i++) { tr.increaseCount(words[i], 1); } double start = getTimeInMillis(); for(unsigned int i = 0; i < tr.numEntries(); i++) { std::string word = tr.getNthPopular(i); out << tr.getCount(word) << " " << word << "\n"; } double end = getTimeInMillis(); out.close(); std::cout << "time per item: " << (end - start) / words.size() << " ms " << std::endl << "total time: " << (end - start) << " ms" << std::endl << std::endl; }
/** * This tests a simple (but unlikely) use case, which is to read in all the data, and then print out the data in sorted order * based on popularity. * * Compare your 28885.txt.out to 28885_txt.out, using diff,s to see if your code is producing correct output. */ double useCase_addAllThenGetInOrder(){ Trends* tr = new smarterTrends(); //You will need to change this to match your own class! std::vector<std::string> wordlist = getWordList("data/28885.txt"); //We only want to time how long addToTrends takes, so we get // the starting time, which is the clock time, in milliseconds double start = getTimeInMillis(); //Now add all the words to the Trends data structure for(unsigned int i=0; i<wordlist.size(); i++){ tr->increaseCount(wordlist[i],1); } //Now get the end time double end = getTimeInMillis(); std::cout << "increaseCount time: " << (end-start)/wordlist.size() << " ms per word" << std::endl; //Now we will print out the complete results. This could be REALLY clow, if // your getNthPopular is not a little bit smart. std::string outfname = "data/28885.txt.out"; std::ofstream out(outfname.c_str()); start = getTimeInMillis(); for(unsigned int i=0; i< tr->numEntries(); i++){ std::string s = tr->getNthPopular(i); out << tr->getCount(s) << ": " << s << std::endl; } out.close(); end = getTimeInMillis(); std::cout << "getNth followed by getCount, time: " << (end - start) / tr->numEntries() << " ms per entry" << std::endl; delete tr; return end - start; }
long currentTimeInMillis() { struct timeval now; gettimeofday(&now, NULL); return getTimeInMillis(&now); }
UBool Calendar::operator==(const Calendar& that) const { UErrorCode status = U_ZERO_ERROR; return isEquivalentTo(that) && getTimeInMillis(status) == that.getTimeInMillis(status) && U_SUCCESS(status); }
void test_adding(void) { std::cout << "test: add all words" << std::endl; std::vector<std::string> words = getWordList("data/28885.txt"); TRENDS_CLASS tr; double start = getTimeInMillis(); for(unsigned int i = 0; i < words.size(); i++){ tr.increaseCount(words[i], 1); } double end = getTimeInMillis(); std::cout << "time per item: " << (end - start) / words.size() << " ms " << std::endl << "total time: " << (end - start) << " ms" << std::endl << std::endl; }
/** * Determines whether this object uses the fixed-cycle Islamic civil calendar * or an approximation of the religious, astronomical calendar. * * @param beCivil <code>true</code> to use the civil calendar, * <code>false</code> to use the astronomical calendar. * @draft ICU 2.4 */ void IslamicCalendar::setCivil(ECivil beCivil, UErrorCode &status) { if (civil != beCivil) { // The fields of the calendar will become invalid, because the calendar // rules are different UDate m = getTimeInMillis(status); civil = beCivil; clear(); setTimeInMillis(m, status); } }
void IslamicCalendar::setCalculationType(ECalculationType type, UErrorCode &status) { if (cType != type) { // The fields of the calendar will become invalid, because the calendar // rules are different UDate m = getTimeInMillis(status); cType = type; clear(); setTimeInMillis(m, status); } }
int main(void) { std::cout << "testing: " << TESTING << std::endl << std::endl; double start = getTimeInMillis(); test_adding(); test_add_all_increment_all(); test_add_all_increment_get_pop(); test_print_words_in_order(); test_print_words_in_order_with_count(); test_add_get_most_pop(); test_add_get_count_of_word(); test_add_all_get_count(); test_hardest(); double end = getTimeInMillis(); std::cout << "total test time: " << (end - start) << " ms" << std::endl; return 0; }
void test_add_get_count_of_word(void) { std::cout << "test: add a word then get the count of that word" << std::endl; std::vector<std::string> words = getWordList("data/28885.txt"); std::ofstream out("data/28885.txt.out" + OUT_FILE); TRENDS_CLASS tr; double start = getTimeInMillis(); for(unsigned int i = 0; i < words.size(); i++){ tr.increaseCount(words[i], 1); unsigned int value = tr.getCount(words[i]); out << words[i] << " " << value << "\n"; } double end = getTimeInMillis(); out.close(); std::cout << "time per item: " << (end - start) / words.size() << " ms " << std::endl << "total time: " << (end - start) << " ms" << std::endl << std::endl; }
void mainLoop(GLuint &vertexbuffer, GLuint &glslProgramID, GLuint &matrixID, glm::mat4 &mvp) { long start, end; start = getTimeInMillis(); end = getTimeInMillis(); bool running = true; while (running) { // limited to 60 fps if (end - start > 16) { start = getTimeInMillis(); // clear the screen glClear(GL_COLOR_BUFFER_BIT); // use our shaders glUseProgram(glslProgramID); // send our transofrmation to currently bound shader, // in the "MVP" uniform // for each model that is rendered, since the MVP will be different // (atleast model part) glUniformMatrix4fv(matrixID, 1, GL_FALSE, &mvp[0][0]); drawTriangle(vertexbuffer); glfwSwapBuffers(); // swap buffers if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS || !glfwGetWindowParam(GLFW_OPENED)) { running = false; } } end = getTimeInMillis(); } }
int32_t Calendar::fieldDifference(UDate targetMs, UCalendarDateFields field, UErrorCode& ec) { if (U_FAILURE(ec)) return 0; int32_t min = 0; double startMs = getTimeInMillis(ec); // Always add from the start millis. This accomodates // operations like adding years from February 29, 2000 up to // February 29, 2004. If 1, 1, 1, 1 is added to the year // field, the DOM gets pinned to 28 and stays there, giving an // incorrect DOM difference of 1. We have to add 1, reset, 2, // reset, 3, reset, 4. if (startMs < targetMs) { int32_t max = 1; // Find a value that is too large while (U_SUCCESS(ec)) { setTimeInMillis(startMs, ec); add(field, max, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return max; } else if (ms > targetMs) { break; } else { max <<= 1; if (max < 0) { // Field difference too large to fit into int32_t ec = U_ILLEGAL_ARGUMENT_ERROR; } } } // Do a binary search while ((max - min) > 1 && U_SUCCESS(ec)) { int32_t t = (min + max) / 2; setTimeInMillis(startMs, ec); add(field, t, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return t; } else if (ms > targetMs) { max = t; } else { min = t; } } } else if (startMs > targetMs) { int32_t max = -1; // Find a value that is too small while (U_SUCCESS(ec)) { setTimeInMillis(startMs, ec); add(field, max, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return max; } else if (ms < targetMs) { break; } else { max <<= 1; if (max == 0) { // Field difference too large to fit into int32_t ec = U_ILLEGAL_ARGUMENT_ERROR; } } } // Do a binary search while ((min - max) > 1 && U_SUCCESS(ec)) { int32_t t = (min + max) / 2; setTimeInMillis(startMs, ec); add(field, t, ec); double ms = getTimeInMillis(ec); if (ms == targetMs) { return t; } else if (ms < targetMs) { max = t; } else { min = t; } } } // Set calendar to end point setTimeInMillis(startMs, ec); add(field, min, ec); /* Test for buffer overflows */ if(U_FAILURE(ec)) { return 0; } return min; }
UBool Calendar::after(const Calendar& when, UErrorCode& status) const { return (this != &when && getTimeInMillis(status) > when.getTimeInMillis(status)); }
// Read from FSA to detect new sensors being attached to datalogger. void detect_new_sensors(int fd) { int i,n,write_chr; int ec; int mptr = 1; int can_check_fsa = 1; int new_data; FSA_Status fsa_status; typedef struct Sensor_s { int valid; char *type; char *table_name; // Added by Vinay // Start SensorMetaData* smd; // End } Sensor; // This array is indexed by array ID - 1; static Sensor sensors[MAX_ARRAY_ID]; for (int i = 0; i < MAX_ARRAY_ID; i++) { sensors[i].valid = 0; sensors[i].type = NULL; sensors[i].table_name = NULL; // Added by Vinay // Start sensors[i].smd = NULL; // End } int array_id = 1; // Initialize the sensor StatusInfo struct struct StatusInfo sen_status[10]; for (i = 0; i < 10; ++i) { sen_status[i].present = 0; sen_status[i].address = i; } METADATA *metadata; int flag = 1; //Continuously check for new data in the FSA while (1) { sleep(1); //Check the status of the Final Storage Area fsa_status = get_fsa_status(fd); //If the dsp and mptr are different, then we have new data in the FSA new_data = fsa_status.DSP_location - mptr; if (new_data > 0) { //Manually set the mptr printf("\n*** New data in FSA ***\n"); printf("\nDSP is %d\n", fsa_status.DSP_location); printf("\nMPTR is %d\n", mptr); set_mptr(fd, mptr); //Get the new binary data char dump[new_data*2 + 2]; get_binary_dump(fd, new_data, dump); //Decode the binary data int n_arrays; const Cdl_Array *arrays = decode_binary_dump(dump, new_data*2 + 2 , &n_arrays); //present or not present values const double pres = 0.0; const double not_pres = -99999.0; for (int i = 0; i < n_arrays; i++) { const Cdl_Array *a = &arrays[i]; printf("array id: %d, n: %d\n", a->id, a->length); for (int j = 0; j < a->length; j++) { Cdl_Element *e = &a->elements[j]; switch (e->type) { case CDL_ET_LO_RES: case CDL_ET_HI_RES: printf("element %d: %f\n", j, e->value.dble); break; case CDL_ET_STRING: printf("element %d: %s\n", j, e->value.strng); break; default: assert(0); abort(); } } if (a->id == POLLING_RESULTS_ARRAY_ID) { // If truncated array, just ignore it. // XXX - Length should not be hard-coded. if (a->length < 12) { fprintf(stderr, "WARNING: Truncated array.\n"); continue; } Cdl_Element *e = &a->elements[0]; if (e->type != CDL_ET_LO_RES) { fprintf(stderr, "WARNING: Time format unexpected.\n"); continue; } double start_time = e->value.dble; printf("start time is %f\n", start_time); int first_status = -1; for (int i = 1; i < a->length; i++) { if (a->elements[i].type != CDL_ET_STRING) { first_status = i + 1; break; } } if (first_status == -1) { fprintf(stderr, "WARNING: Couldn't find second timestamp.\n"); continue; } e = &a->elements[first_status - 1]; if (e->type != CDL_ET_LO_RES) { fprintf(stderr, "WARNING: Time format unexpected.\n"); continue; } double stop_time = e->value.dble; printf("Stop time is %f\n", stop_time); if (first_status + 10 > a->length) { fprintf(stderr, "WARNING: Array is truncated.\n"); continue; } // Traverse the ten "present or not present" data elements. // When the value of the element is 0.0, then there is a // sensor present. So we set the StatusInfo to present and // read the sensorID that is located at cur_id. We // increment to the next id with cur_id and move on If the // value of the element is -99999.0, then the sensor is not // present. Set the StatusInfo to not present and move on. int cur_id = 1; // Current index into ID region of array. for (int i = 0; i < 10; i++) { Cdl_Element *e = &a->elements[i + first_status]; assert(e->type == CDL_ET_HI_RES); if (e->value.dble == pres) { sen_status[i].present = 1; strcpy(sen_status[i].id, a->elements[cur_id].value.strng); // XXX - Need to remove only trailing spaces, but // for demo, remove all spaces. -kec remove_spaces(sen_status[i].id); printf("sensor %s present at address %d\n", a->elements[cur_id].value.strng, i); cur_id++; } else if (e->value.dble == not_pres) { sen_status[i].present = 0; sen_status[i].id[0] = '\0'; printf("sensor not present at address %d\n", i); } else { fprintf(stderr, "unknown value for checking sensor"); abort(); } } } else { assert(a->id >= 1 && a->id <= 500); const Sensor *s = &sensors[a->id - 1]; if (s->valid) { printf("array id: %d, n: %d\n", a->id, a->length); for (int j = 0; j < a->length; j++) { Cdl_Element *e = &a->elements[j]; switch (e->type) { case CDL_ET_LO_RES: case CDL_ET_HI_RES: printf("element %d: %f\n", j, e->value.dble); break; case CDL_ET_STRING: printf("element %d: %s\n", j, e->value.strng); break; default: assert(0); abort(); } } if (strcmp(s->type, "Greenspan") == 0) { printf("Got data from greenspan.\n"); double data[4]; for (int i = 0; i < 4; i++) { Cdl_Element *e = &a->elements[i]; assert(e->type == CDL_ET_HI_RES); data[i] = e->value.dble; } char buf[100]; char tb[100]; { time_t t = time(0); struct tm tm; localtime_r(&t, &tm); strftime(tb, sizeof tb, "%Y-%m-%d %H:%M:%S", &tm); } sprintf(buf, "'%s',%f,%f,%f,%f", tb, data[0], data[1], data[2], data[3]); printf("Name: %s, value: %s\n", s->table_name, buf); // Altered by Vinay // Start // Commented 2 line below // int ec = insertData(s->table_name, buf); // assert(ec); // Added the below code { int bufSize = sizeof(double)*4; double* dataBuffer = (double*)malloc(bufSize); long long timestamp = getTimeInMillis(); for (int i = 0; i < 4; i++) { dataBuffer[i] = (double) data[i]; } printf("Sending the data to the connector...\n"); sendData(s->smd, dataBuffer, bufSize, timestamp); printf("Done\n"); free(dataBuffer); } // End } } else { printf("WARNING: array id doesn't correspond.\n"); } } } //Free up memory for (int i = 0; i < n_arrays; i++) { Cdl_Array__dtor(&arrays[i]); } free((void *) arrays); //print out struct for (int i = 0; i < 10; i++) { printf("--Address %d--\n", i); printf("address = %d\n", sen_status[i].address); printf("present = %d\n", sen_status[i].present); printf("id = %s\n", sen_status[i].id); } int num; //Call webservice // XXX - The number of status should not be hard-coded. metadata = update_sensor_list(sen_status, 10, &flag, &num); if(metadata == NULL){ printf("No information found\n"); } else { printf("\nNumber of sensors %d\n",num); for(int i = 0; i< num; i++) { printf("\n Sensor ID %s",metadata[i]->sensorID); printf("\n Lake ID %s",metadata[i]->lakeID); printf("\n Logger ID %s",metadata[i]->loggerID); printf("\n Buoy ID %s",metadata[i]->buoyID); printf("\n TableName %s",metadata[i]->tableName); printf("\n SampleRate %s\n",metadata[i]->sampleRate); printf("\n sensorType %s\n",metadata[i]->sensorType); for(int j = 0; j < MAX_NUM_IDS; j++) { if(strcmp(metadata[i]->dataID[j],"")) printf("Data ID %s\n",metadata[i]->dataID[j]); } } /* * Generate new datalogger program for reading data. */ { for (int i = 0; i < MAX_ARRAY_ID; i++) { sensors[i].valid = 0; free(sensors[i].type); free(sensors[i].table_name); sensors[i].table_name = NULL; // Added by Vinay // Start sensors[i].smd = NULL; // End } Cdl_Program input, output; Cdl_Program__ctor(&input, 10); Cdl_Program__ctor(&output, 5); // Set hi-res output. Cdl_Program__p78(&input, 1); // Turn on output flag. Cdl_Program__p86(&input, 10); int loc = 1; for (int i = 0; i < num; i++) { const struct Metadata *const m = metadata[i]; sensors[array_id - 1].valid = 1; sensors[array_id - 1].type = strdupe(m->sensorType); sensors[array_id - 1].table_name = strdupe(m->tableName); // Added by Vinay // Start sensors[array_id - 1].smd = m->smd; // End if (strcmp(m->sensorType, "Greenspan") == 0) { printf("greenspan detected.\n"); Cdl_Program__p105(&input, // XXXX - Fix hard-coded address. m->address, // Address 0, // Command 8, // Port loc, 1, 0); Cdl_Program__p80(&input, 1, array_id); Cdl_Program__p70(&input, 4, loc); loc += 4; printf("greenspan detected: 2.\n"); } else if (strcmp(metadata[i]->sensorType, "TempLine thermistor chain") == 0) { printf("greenspan deteced.\n"); } else { assert(0); abort(); } array_id++; assert(array_id <= 501); if (array_id == 501) { array_id = 1; } } Cdl_String tbl1_s = Cdl_Program__String(&input); Cdl_String s; Cdl_String__ctor(&s); Cdl_String__fcatl(&s, "MODE 1"); Cdl_String__cat(&s, tbl1_s.str); Cdl_String__cat(&s, program_ten_locations); printf("Program: %s\n", s.str); // Upload program. //load_Program(fd, 1, &input); load_program(fd, s.str); Cdl_Program__dtor(&input); // Skip old data. FSA_Status st = get_fsa_status(fd); mptr = st.DSP_location; goto end_cycle; } } mptr += new_data; //increment mptr past the binary data that we just read } //printf("\n-------------------------------------\n"); end_cycle:; } }