Last Time Stamp not within valid period - objective-c

Am rather newbie to Objective C (from Java background):
I got the following code to check if the last time stamp are within the minimum period, how do I do the opposite? E.g. I want to check if the last time stamp is NOT within a valid period, the valid period are in seconds.
[lastTimestamp timeIntervalSinceNow] > -MINIMUM_TIME

This seems too obvious:
if ([lastTimestamp timeIntervalSinceNow] <= -MINIMUM_TIME)
{
// execute some code
}
Where MINIMUM_TIME is a NSTimeInterval variable, representing the time you want to check against.

Related

Create a variable to count from 1 to n in AnyLogic

I am looking to add a variable to count from 1 to 217 every hour in AnyLogic, in order to use as a choice condition to set a parameters row reference.
I am assuming I either need to use an event or a state chart however I am really struggling with the exact and cannot find anything online.
If you have any tips please let me know, any help would be appreciated
Thank you,
Tash
A state machine isn't necessary in this case as this can be achieve using a calculation or a timed event. AnyLogic has time() function which returns time since model start as a double in model time units of measurements.
For example: if model time units is seconds and it has been running for 2hr 2min 10sec then time(SECOND) will return 7330.0 (it is always a double value). 1/217th of an hour corresponds to about 3600/217 = 16.58 seconds. Also, java has a handy function Math.floor() which rounds down a double value, so Math.floor(8.37) = 8.0.
Assembling it all together:
// how many full hours have elapsed from the start of the model
double fullHrsFromStart = Math.floor(time(HOUR));
// how many seconds have elapsed in the current model hour
double secondsInCurrentHour = time(SECOND) - fullHrsFromStart * 3600.0;
// how many full 16.58 (1/217th of an hour) intervals have elapsed
int fullIntervals = (int)(secondsInCurrentHour / 16.58);
This can be packaged into a function and called any time and it is pretty fast.
Alternatively: an Event can be created which increments some count by 1 every 16.58 seconds and ten resets it back to 0 when the count reaches 217.

How can I have the total time Psychopy

I made an experimentation on Psychopy. I have Intructions, 10 differents routines and then msg end.
I am able to have all the time it took for each routine, but I will like to have the total time of my 10 routines without having to calculate it my self in my csv file at the end.
Code for having my duration of each trial. I putted this line in each routine.
thisExp.addData('trial_duration1', t)
I tried to create a variable total and adding all the trial_duration, but my column was empty in the csv file at the end.
Thanks!!
Psychopy has an internal clock which starts when the experiment starts. You can read the time using core.monotonicClock.getTime(). The timing of this clock starts almost immediately as you hit "run", i.e. before the dialogue box, so it doesn't read the time since the first routine started. However, you can get that duration by first recording the time of the clock in a code component when you want time zero to be defined:
time_zero = core.monotonicClock.getTime()
... and then record the time elapsed doing
thisExp.addData('cumulative_duration', core.monotonicClock.getTime() - time_zero)
Note that if you want to do this only for particular loops (e.g. define time_zero in the first loop and record cumulative_duration in the last loop) require the condition to be satisfied:
# If this is the first iteration of the loop (no matter the name of the loop)
if currentLoop.thisN == 0:
time_zero = core.monotonicClock.getTime()

Demonstrate the difference in performance of function returning value and function not returning any value both function with identical codes

As a curiosity i held one experiment in objective C:
My assumtion before starting the experiment was function returning value should take more time (Nanoseconds) than function with no returns.
Two functions having same number and same codes but one returning value and another not returning the value were written.
-(void)methodNotReturningTheValue
{
//Some Codes + #""
}
-(NSString*)methodReturningTheValue
{
//Some Codes
return #"";
}
The time is shown in nano second before and after calling the function. Below are the results:
Example:
Time Before Calling methodNotReturningTheValue: 1411033150.946451
Time Before Calling methodReturningTheValue: 1411033150.946978
Difference in Time (Before and after) for methodReturningTheValue:0.000527
Time Before Calling methodReturningTheValue: 1411033150.947947
Time Before Calling methodReturningTheValue: 1411033150.948464
Difference in Time (Before and after) for methodReturningTheValue: 0.000517
The results are not consisted. Sometime time consume by methodreturningvalue is greater and sometime methodNotReturning value is greater. May be the fluctuation in working of [[NSDate date] timeIntervalSince1970]; is not allowing the experiment to calculate the time consumed by function returning and not returning values.
am i sounding irrelative or any direction or solution to solve this curiosity:
My Query: How to demonstrate performance difference between function returning value and function with out any return (Void) in any language?
Thank You!
A value returned from a method is stored in a register (at least under ARM and <= 16-bytes; see the accepted answer to this question) and so that difference between the methods is irrelevant when a reference is returned.
A method returning a struct on the other hand will be more relevant, given the struct must be copied back to the caller.

Unstable NSTimer causes fluctuations in counting

I use NSTimer to count from a certain moment.
int totalSeconds;
int totalMinutes;
int totalHours;
If the totalSeconds are 60, totalMinuts become +1. Its very simple and should work.
For example i started the NSTimer together with the clock of my mac. (running on simulator).
When i look at the clock of my mac and the timer and compare the time the first 10-20 seconds its counting perfectly synchronous. After that it fluctuates or goes ahead 5 seconds or more.
So i output my timer and found this:
2012-10-24 14:45:44.002 xxApp driveTime: 0:0:44
2012-10-24 14:45:45.002 xxApp driveTime: 0:0:45
2012-10-24 14:45:45.813 xxApp driveTime: 0:0:46
2012-10-24 14:45:46.002 xxApp driveTime: 0:0:47
The milliseconds are timed at 002 as you see. But at the third row its 813. This happens very randomly and causes the fluctuations.
Is there a more stable way to count?
From the NSTimer documentation
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds.
If your goal is to compute the total time that has passed since your program has started running, this is quite easy. As soon as you want to begin keeping track of time, store -[NSDate date] into a variable. Whenever you want to compute how much time has passed, call -[NSDate date again and do the following, assuming originalDate is a property where you stored the result of the first call to -[NSDate date]:
NSDate *presentDate = [NSDate date];
NSTimeInterval runningTime = [presentDate timeIntervalSinceDate:originalDate];
runningTime will be the total number of seconds that have elapsed since you started keeping track of time. In order to get the number of hours, minutes, seconds, and so on, an NSDateComponents object should be used.
This mechanism will allow you to use a timer to update your total running time "just about once a second" without losing accuracy.

Incrementing a double is never whole

In Objective-C I have a timer fire every 0.1 seconds and increment a double value (seconds) by 0.1.
So it should basically keep time counting up by 1/10 of a second. When it fires it checks some if-else statements to see if time (seconds) is equal to 3, 9, 33, etc., but these are never triggered. I suppose it is because of the way doubles are represented in bits, that is the decimal is an approximation and never actually a whole number.
How can I fix this so my statements are triggered?
-(void)timeSeconds:(NSTimer*)theTimer {
seconds = seconds + 0.1;
NSLog(#"%f", seconds);
if (seconds == 3.0) {
[player pause];
[secondsTimer invalidate];
}
else if (seconds == 9){
[player pause];
[secondsTimer invalidate];
}
The floating point types cannot represent some numbers exactly, so when these are added, the error is compounded and the floating point type becomes less and less precise.
Use an integral type but represent the time difference using greater resolution, for example, use an NSUInteger to represent milliseconds instead of seconds, and increment by 100 instead of 0.1. Instead of comparing seconds == 3.0, you would use milliseconds == 3000, etc.
Keep in mind that timers are not fired very precisely:
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed.
You may find that when milliseconds==9000, more than 9 seconds has actually passed (but probably not much more). There are other tools available if more precise timing is required.
0.1 cannot be represented exactly in binary floating point, so you get a small error that accumulates over time. If you want an exact value, use an int or long variable that get incremented by 1 each time called tenthsOfSeconds.
Floating point math is imprecise, using floating to count is not a great idea, but if you must, check that the difference between the count and the variable is very small.