Pastey rotating_phrase.ex
- Posted by SDPringle Mar 22, 2012
-- rotating_phrase.ex -- should take about one minute include std/datetime.e include std/locale.e as locale include std/console.e as con include std/get.e as parse include std/stats.e as stats include std/cmdline.e as cmd include std/map.e as map constant SAMPLE_TIME = 3 -- seconds constant Command_line = command_line() type truth_type(integer x) return x = 1 end type integer target_time = 60 -- seconds integer len = length("hello world") map:map m = cmd_parse( { { 0, "time", "set the time for the test", {HAS_PARAMETER}}, { "l", "length", "set the sequence length for prepends", {HAS_PARAMETER}, routine_id("parse_length")} } ) if map:has(m,"length") then sequence buffer = parse:value(map:get(m,"length")) if buffer[1] = GET_SUCCESS then len = buffer[2] end if end if if map:has(m,"time") then sequence buffer = parse:value(map:get(m,"time")) if buffer[1] = parse:GET_SUCCESS then target_time = buffer[2] end if end if if SAMPLE_TIME >= target_time then printf(2, "Please provide more time for this test.") end if function delayed_time() -- We want to get start_t when time() has just ticked over for consistency. atom t = time() while t = time() do end while return time() end function sequence rotating_phrase = rand(repeat({26}, len))+'A'-1 sequence current_time = date() current_time = current_time[4..6] printf(1, "Will now rotate a shallow string of length %s\n", {number(len)}) if target_time > 60 then printf(1, "Test should take about %s minutes. The time is %02d:%02d:%02d.\n", {number(target_time/60)} & current_time) else printf(1, "Test should take about %s seconds. The time is %02d:%02d:%02d.\n", {number(target_time)} & current_time) end if -- First, determine an appropriate number of trials for this computer. -- trials is how many trials were done in ten seconds integer trials = 0 integer len_1 = len-1 object temp_char atom start_t = delayed_time() atom end_t = start_t + SAMPLE_TIME while time() < end_t do temp_char = rotating_phrase[$] rotating_phrase = head(rotating_phrase, len_1) rotating_phrase = prepend(rotating_phrase, temp_char) trials += 1 end while -- set trials so that following loops should take up the remaining time -- we neglect the time it takes to run head trial times and the increment -- loops as if they were instantaneous because head and increment is quite fast. trials = floor((target_time / SAMPLE_TIME - 1) * trials) printf(1, "Using %s trials.\n", {number(trials)}) -- Time how long it takes to increment temp_char = 0 start_t = delayed_time() for k = 1 to trials do temp_char += 1 end for end_t = time() object to_increment = end_t - start_t -- Now, run a loop with the appropriate number of trials start_t = delayed_time() for k = 1 to trials do temp_char = rotating_phrase[$] rotating_phrase = head(rotating_phrase, len_1) rotating_phrase = prepend(rotating_phrase, temp_char) end for end_t = time() atom to_rotate = end_t - start_t -- Time how long it takes to increment temp_char = 0 start_t = delayed_time() for k = 1 to trials do temp_char += 1 end for end_t = time() to_increment &= end_t - start_t -- determine how long it takes to take the len head trials times -- we cannot use len-1 because that would change rotating_phrase by -- the end of each iteration. Hopefully, this doesn't change things too much. start_t = delayed_time() for k = 1 to trials do temp_char = rotating_phrase[$] rotating_phrase = head(rotating_phrase, len) end for end_t = time() atom to_head = end_t - start_t -- Time how long it takes to increment temp_char = 0 start_t = delayed_time() for k = 1 to trials do temp_char += 1 end for end_t = time() to_increment &= end_t - start_t ? to_increment to_increment = average(to_increment) atom to_prepend = to_rotate - to_head if to_rotate != 0 or to_head != 0 or to_prepend != 0 then if to_head then printf(1, "Speed is %s heads/second.\n", {number(trials / to_head)}) else printf(1, "No time was recorded to head %s times.\n", {number(trials)}) end if if to_rotate then printf(1, "Speed is %s rotations/second.\n", {number(trials / to_rotate)}) else printf(1,"No time was recorded to rotate %s times.\n", {number(trials)}) end if if to_prepend then printf(1, "Speed is %s prepends/second.\n", { number(trials / (to_prepend)) }) else printf(1, "There was no difference calculated for rotate and head of a sequence %s times.\n", {number(trials)}) end if if to_increment then printf(1, "Prepend takes as much time as %s increment operations.\n", {number(to_prepend / to_increment)}) end if else printf(1, "Sample set too small to calculate the rate of speed.\n") end if current_time = date() current_time = current_time[4..6] printf(1, "The date is %02d:%02d:%02d.\n", current_time) sequence results integer ifd ifd = open("timings.dat","r") if ifd != -1 then sequence buffer = parse:get(ifd) if buffer[1] = GET_SUCCESS then results = buffer[2] else results = {} end if close(ifd) else results = {} end if integer ofd ofd = open("timings.dat","w") if ofd != -1 then results = append(results,{Command_line[1], len, trials / to_rotate}) print(ofd, results) close(ofd) end if


