ArrayFire af::where time consumption - arrayfire

I have an af::array for which i want to find the non zero location (2048*2048).
af::timer start1 = af::timer::start();
af::array index = af::where(mat); // for the first time
cout << "elapsed time (ms): " << af::timer::stop(start1)*1000 <<" ms"<<endl;
start1 = af::timer::start();
index = af::where(mat); //for the second time
cout << "elapsed time (ms): " << af::timer::stop(start1)*1000 <<" ms"<<endl;
First iteration:
elapsed time (ms) : 1.8792 ms
elapsed time (ms) : 0.7094 ms
Second iteration:
elapsed time (ms) : 16.8074 ms
elapsed time (ms) : 0.4738 ms
Third iteration
elpased time (ms) : 17.3236 ms
elpased time (ms) : 0.4543 ms
Why are the times is so inconsistent?

Related

This code causes Corona to become unresponsive

When I run this code in Corona on Windows 7 it instantly crashes. It works fine in ZeroBrane. Any ideas why?
--Stopwatch--
local startTime
function start()
startTime = os.time()
--Start the stop watch--
end
function secondsEllapsed()
--Return the number of seconds since the stop watch was started--
return os.time() - startTime
end
start()
while true do
-- Get the time ellapsed and convert it to hours, minutes and seconds
ellapsed = secondsEllapsed()
hours = math.floor(ellapsed / 3600)
minutes = math.floor((ellapsed - (hours * 3600)) / 60)
seconds = math.floor((ellapsed - (hours * 3600) - (minutes * 60)))
-- Print the time ellapsed to the command line
print(hours .. 'h', minutes .. 'm', seconds .. 's')
end
It crashes probably because you run infinity loop.

lua number format not working

I have the following function:
function timestamp(duration)
local hours = duration / 3600
local minutes = duration % 3600 / 60
local seconds = duration % 60
return string.format("%02d:%02d:%02.03f", hours, minutes, seconds)
end
when the duration is 4.404 sec it returns 00:00:4.404
what is am looking for is 00:00:04.404
It should be:
string.format("%02d:%02d:%06.3f", hours, minutes, seconds)
Field width contains all characters of the number, including point and fraction.

I made a score counter but it is late [duplicate]

I am using timer.performWithDelay to time how long it takes a player to complete a level. I want it to measure down to the 100th of a second (because the game is multiplayer, and I don't want there to be too many ties).
Here is what I did:
local totaltime = 0
local function counter()
totaltime = totaltime + 0.01
print(totaltime)
end
timer1 = timer.performWithDelay( 10, counter, 0)
It results in each "second" lasting about 4 seconds. Is this just not practical or is there a flaw somewhere?
When timer.preformWithDelay is given a time delay smaller then the time between your frames the timer will wait until the next frame is entered to call the function.
That means if you have a game running at 30 or 60 fps, you would have a 'frame ms' of about 16 or 33ms. So the minimum delay you can put is the delay between your frames.
In your case you want to set your timer every 1/100th of a second, or with 10ms. This means, since your frame is most likely 16ms (60fps), that every logged 10ms you are actually waiting an addional 6ms.
Now you could solve this if you ran with 100 FPS and thus achieved said 10 ms, but this is NOT recommendable.
AlanPlantPot provided the answer for following solution on coronaLabs:
I would use the enterFrame function instead. Your timer won't go up in single milliseconds (it will increase by however many ms have passed in each frame), but nobody would be able to read that fast anyway.
local prevFrameTime, currentFrameTime --both nil
local deltaFrameTime = 0
local totalTime = 0
local txt_counter = display.newText( totalTime, 0, 0, native.systemFont, 50 )
txt_counter.x = 150
txt_counter.y = 288
txt_counter:setTextColor( 255, 255, 255 )
group:insert( txt_counter )
and
local function enterFrame(e)
local currentFrameTime = system.getTimer()
--if this is still nil, then it is the first frame
--so no need to perform calculation
if prevFrameTime then
--calculate how many milliseconds since last frame
deltaFrameTime = currentFrameTime - prevFrameTime
end
prevFrameTime = currentFrameTime
--this is the total time in milliseconds
totalTime = totalTime + deltaFrameTime
--multiply by 0.001 to get time in seconds
txt_counter.text = totalTime * 0.001
end

CoronaSDK - Implementing game timer counting milliseconds

I am using timer.performWithDelay to time how long it takes a player to complete a level. I want it to measure down to the 100th of a second (because the game is multiplayer, and I don't want there to be too many ties).
Here is what I did:
local totaltime = 0
local function counter()
totaltime = totaltime + 0.01
print(totaltime)
end
timer1 = timer.performWithDelay( 10, counter, 0)
It results in each "second" lasting about 4 seconds. Is this just not practical or is there a flaw somewhere?
When timer.preformWithDelay is given a time delay smaller then the time between your frames the timer will wait until the next frame is entered to call the function.
That means if you have a game running at 30 or 60 fps, you would have a 'frame ms' of about 16 or 33ms. So the minimum delay you can put is the delay between your frames.
In your case you want to set your timer every 1/100th of a second, or with 10ms. This means, since your frame is most likely 16ms (60fps), that every logged 10ms you are actually waiting an addional 6ms.
Now you could solve this if you ran with 100 FPS and thus achieved said 10 ms, but this is NOT recommendable.
AlanPlantPot provided the answer for following solution on coronaLabs:
I would use the enterFrame function instead. Your timer won't go up in single milliseconds (it will increase by however many ms have passed in each frame), but nobody would be able to read that fast anyway.
local prevFrameTime, currentFrameTime --both nil
local deltaFrameTime = 0
local totalTime = 0
local txt_counter = display.newText( totalTime, 0, 0, native.systemFont, 50 )
txt_counter.x = 150
txt_counter.y = 288
txt_counter:setTextColor( 255, 255, 255 )
group:insert( txt_counter )
and
local function enterFrame(e)
local currentFrameTime = system.getTimer()
--if this is still nil, then it is the first frame
--so no need to perform calculation
if prevFrameTime then
--calculate how many milliseconds since last frame
deltaFrameTime = currentFrameTime - prevFrameTime
end
prevFrameTime = currentFrameTime
--this is the total time in milliseconds
totalTime = totalTime + deltaFrameTime
--multiply by 0.001 to get time in seconds
txt_counter.text = totalTime * 0.001
end

change the TimeStamp return into mm:sec not hour

I have this:
<(Time.now).to_i>
it returns a integer thing
and want to convert that integer value into (mins and seconds)
only as per my requirement
Try this
Time.now.strftime("%M:%S")
Hope you are trying to find this
You can get the minutes and seconds by this
delta = Time.now.to_i
%w[ weeks days hours minutes seconds].collect do |step|
seconds = 1.send(step)
(delta / seconds).to_i.tap do
delta %= seconds
end
end
It will return an array with having 5 elements . You can get the mins and secs from
arr[3] and arr[4]
Also you can take the whatever the time in integer format as delta .
If you want to show only mins and secs when hr = 0
#hr = timedef[0]
#mn = timedef[1]
#sec = timedef[2]
time_remains = ''
unless self.timedef.blank?
if #hr > 0
time_remains = time_remains + "#{#hr} #{'hour'.pluralize(#hr)} "
elsif #mn > 0
time_remains = time_remains + "#{#mn} #{'minute'.pluralize(#mn)} and #{#sec} #{'second'.pluralize(#sec)} "
else
time_remains = time_remains + "#{#sec} #{'second'.pluralize(#sec)} "
end
end
use strftime -- see this page http://www.dzone.com/snippets/date-time-format-ruby it has the formats (though its similar to c/c++ and Java)
Also, you will likely want to do some research into timezone handling on the time class. there is a function: in_time_zone that can convert the timezone for you. So typically you store the times or the times fetched are in UTC then you can dynamically change the timezone before you strftime.

Resources