iConcept : What is DOM ?

 

What is DOM (Document Object Model): Tree and Node Structure of HTML Page

The Document Object Model (DOM) defines a standard way for accessing and manipulating HTML documents. The DOM presents an HTML document as a TREE-STRUCTURE. The DOM is a W3C (World Wide Web Consortium) standard. The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.

The Document Object Model (DOM) is the model that describes how all elements in an HTML page, like input fields, images, paragraphs etc., are related to the topmost structure: the document itself.
DOM: The Tree and Node Structure
The HTML DOM views an HTML document as a tree-structure. The tree structure is called a node-tree. In the DOM, everything in an HTML document is a node. All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.
Note: The Root Node of DOM Tree is DOCUMENT.
document.documentElement – returns the root node of the document
document.body – gives direct access to the <body> tag
The nodes in the node tree have a hierarchical relationship to each other. On the basis of hierarchy, Nodes can be divided into three categories:
1. Parent Nodes
2. Child Nodes
3. Sibling Nodes:
  Child Nodes on the same level are called siblings (brothers or sisters).
Example:
<p>This is a paragraph</p>
Here “p” is parent node and ‘This is a paragraph’ is a child node.
Note:
1. In a node tree, the top node is called the root
2. Every node, except the root, has exactly one parent node
3. A node can have any number of children
4. A leaf is a node with no children
5. Siblings are nodes with the same parent
Nodes can also be classified as:
1. Element Nodes: Every HTML element is an element node
2. Text Nodes: The text in the HTML elements are text nodes. In the HTML DOM the value of the text node can be accessed by the innerHTML property.
3. Attribute Nodes: Every HTML attribute is an attribute node
4. Comment Nodes: Comments are also treated as nodes in DOM.
5. Document Node: It is the parent node of DOM.. The entire document is a document node
Example:
<p align=”right”>This is a paragraph</p>
Here “p” is element node, ‘This is a paragraph” is a text node and “align” is the attribute node.
Node Properties
In the HTML DOM, each node is an object.Objects have methods and properties that can be accessed and manipulated by JavaScript. Three important node properties are:
1. nodeName
2. nodeValue
3. nodeType
The nodeName Property
The nodeName property specifies the name of a node.
1. nodeName is read-only
2. nodeName of an element node is the same as the tag name
3. nodeName of an attribute node is the attribute name
4. nodeName of a text node is always #text
5. nodeName of the document node is always #document
Note: nodeName always contains the uppercase tag name of an HTML element.
The nodeValue Property
1. The nodeValue property specifies the value of a node.
2. nodeValue for element nodes is undefined
3. nodeValue for text nodes is the text itself
4. nodeValue for attribute nodes is the attribute value
The nodeType Property
The nodeType property returns the type of node. nodeType is read only.
The most important node types are: Element Node, Text Node, Attribute Node, Comment Node and Document Node
HTML DOM Methods:
x.getElementById(id) – get the element with a specified id
x.getElementsByTagName(name) – get all elements with a specified tag name
x.appendChild(node) – insert a child node to x
x.removeChild(node) – remove a child node from x
Note: In the list above, x is a node object (HTML element or node, say document).
HTML DOM Properties
x.innerHTML – the text value of x
x.nodeName – the name of x
x.nodeValue – the value of x
x.parentNode – the parent node of x
x.childNodes – the child nodes of x
x.attributes – the attributes nodes of x
Note: In the list above, x is a node object (HTML element or node, say <p>).
Reference : Click Here

 

iConcept: GET vs POST

 GET vs POST: Which one is better? A 10 point comparison
1. Data Size Restriction in GET: There is a character restriction of 255 in the URL. This is mostly the old browsers restriction and new ones can handle more than that. But we can’t be sure that all our visitors are using new browsers. So when we show a text area or a text box asking users to enter some data, then there will be a problem if more data is entered. This restriction is not there in POST method. We can transfer unlimited data using POST. In PHP by default 8MB of data can be transferred. (can be changed by setting the post_max_size in the php.ini file)
2. Data Type Restriction in GET: As the data transfers through address bar ( URL ) there are some restrictions in using space, some characters like ampersand ( & ) etc in the GET method of posting data. We have to take special care for encoding (while sending) and decoding (while receiving) data if such special characters are present.
3. Security: In GET method data gets transferred to the processing page in name value pairs as a query string in URL, so it is exposed and can be easily traced by visiting history pages of the browser. Data is always submitted in the form of text. So any login details with password should never be posted by using GET method. On the other hand, POST is much more secure. In case of POST, all the name value pairs are submitted in the Message Body of the request.
4. Speed: GET is faster than POST.
5. Bookmarking: There are some special cases where advantage of using GET method is, one can store the name value pairs as bookmark and directly use them by bypassing the form. But you cannot bookmark using POST method.
6. If POST method is used and if the page is refreshed it would prompt before the request is resubmitted but it would not prompt if GET method is used.
7. Uploading files through input type file is possible in POST but not with GET method.
8. There are chances for data lost after server encoding in GET method but no data loss occurs in case of POST method.
9. GET uses STACK method for passing form variables while POST method uses HEAP method for passing form variables.
10. GET can store up to 18 form variables but there is no limit in case of POST method.
Reference : Click Here
Thanks :), Keep Coding:)

iConcept : Dropbox Alternatives and Competitors: Dropbox vs Other Online File Sharing and Storage Tools

Dropbox Alternatives and Competitors: Dropbox vs Other Online File Sharing and Storage Tools

Dropbox is a big name in the online file sharing and storage market. But there are a lot of alternatives and competitors of dropbox present in the online file sharing and storage market.
As there are some security and safety concerns with dropbox, people are searching for alternatives to dropbox to store their data online. A lot of competitors of dropbox are there in software market. We will have a detailed discussion on various alternatives to dropbox and competitors of dropbox. We will mainly focus on the features of Microsoft Live Mesh, Spider Oak, SugarSync, Wuala, Amazon Cloud Drive, Box, Syncplicity, Cubby, Huddle,  Egnyte, ShareFile, TeamDrive, Cloudme, CX etc. These are giving very tough competition to dropbox in syncing data with various devices or networks, online storage space, security and safety settings, encrypted data backup and recovery, media streaming etc.
Here goes the list of Alternatives to Dropbox: Dropbox Competitors
1. Microsoft Live Mesh: Microsoft’s file syncing tool works really well in the background. You can use Live Mesh just for local syncing and/or 5GB of SkyDrive’s online storage space (out of SkyDrive’s 25GB max). As you might expect, Live Mesh has some unique benefits for Windows users: you can remote control your Live Sync connected Windows computer (similar to Remote Desktop Connection) and sync Microsoft Office and Internet Explorer program settings. SkyDrive is also where Office Web Apps online documents are stored, so you get that integration as well.
Storage: Up to 25 GB Free, per SkyDrive Account
Desktop Support: Windows Vista SP2 or later, Mac OSX 10.5 or later
Browser Support: IE 7 or later preferred
Mobile Clients: None
2. SpiderOak: Besides its really strong “zero knowledge” security features, SpiderOak is very versatile: it can sync not only your desktop or mobile device but your external drive or network volume as well—so you can use it to keep your desktop, laptop, and USB thumb drive in sync, for example. SpiderOak offers a lot of information about your backups, uploads, and syncs, so you always feel in control of your data. Refer a friend and you get an additional 1GB more free space (up to 50GB max for the free referral space).
Storage: 2 GB Free, with Personal and Enterprise Plans Available
Desktop Support: PCs with Windows XP or greater, Mac 10.5 or later, and the more popular Linux distributions
Browser Support: Most modern Web browsers
Mobile Clients: iPhone and iPad with iOS 4, Android 2 or later, Blackberry 4 or later
3. SugarSync: SugarSync does what Dropbox does, but lacks the LAN sync and strong API support. It makes up for that in more features for media streaming, mobile syncing (including auto syncing photos and folders from Android devices), and folders selection and permissions/passwords settings. SugarSync’s web interface has a bit more functionality than Dropbox, with its separation of photos from files and also a very useful direct editing feature where you can edit files from within the webapp. You get a generous 5GB free to start with and can earn 500MB more space for each referral.
Storage: 5 GB Free, with Personal and Enterprise Plans Available
Desktop Support: PCs with Windows XP or later and Mac Desktops 10.4 or later
Browser Support: IE, Firefox 3 or later, Safari 4 or later, Chrome
Mobile Clients: iOS 4, Android 2 or later, Blackberry OS 4 or later, Windows Phone 7, and Symbian
4. Wuala: From Swiss storage manufacturer LaCie, Wuala, like SpiderOak, offers locally encrypted data backup and syncing. Although the 1GB of free storage space is on the low end to start with, Wuala has a unique way to earn additional storage: you can buy more space or exchange unused storage on your computer (e.g., empty space on an external drive) for more storage on Wuala.
5. Amazon Cloud Drive: Amazon’s drive offering is particularly attractive if you’re looking for a way to store your music library. They recently added Safari support, so if you’re on an iPhone or iPad you can now use the service. If you’re already set up with a Cloud Drive account, and have some music in it, just point Safari to the Cloud Player sign in page and you’ll see the desktop interface on your iOS device. Sign into your account and you’ll see the desktop interface on your iOS device.
On the downside, there’s no sync service or automated way to add your non-music files. It’s all manual upload and download. This is a fair place to hold your music library, but not useful for much else, given the options available today. Also, being forced to manually upload files via a Web browser gives the product a rushed and unfinished feel. However, it’s hard to complain about a free place to store your music. If you’re looking for a good place to store and stream your music library and can use some rudimentary non-music file storage as well, then Amazon Cloud Drive may work for you.
Storage: 5 GB Free, or 20 GB free with purchase of an album from the Amazon MP3 store
Desktop Support: Any OS that supports Flash
Browser Support: Any browser with Flash 9 or 10, newest browser versions preferred but not required
Mobile Clients: Android 2 or later, and browser support for Apple devices with iOS 4
6. Box: Box.net offers 5 GB of free cloud storage with a 25-MB file size limit. Storage is accessible by only one user, and access to your data via mobile app also is free. Box.net provides paid options for 25 GB and 50 GB at $10 and $20 per month, respectively, with 1-GB file size limits each.
Box.net offers up to 500 GB of shared storage, the most among the products within this roundup, with a collaborative toolset and workspace for up to three users for $15 per user, per month. Enterprise customers with needs beyond three users are encouraged to call Box.net for pricing. The features and functionality offered here are best suited to enterprise customers. Box.net’s complete set of collaborative tools, larger shared storage allotments, and associated costs are geared toward businesses with distributed work teams.
Storage: 5 GB Free
Desktop Support: Windows Vista SP2 or later, Mac OSX 10.5 or later
Browser Support: Mobile browser–Web Kit compatible
Mobile Clients: iOS 4.0 or later, Android 2.0 or later
7. Syncplicity: Syncplicity offers the same sync and share features of Dropbox with a decided enterprise slant. For example, admins can control which devices can access cloud storage inside or outside the company. Data retention policies can help admins find and remove documents that violate company data protection policies.
In a nod to the existing data protection measures in a large enterprise that allows an admin to wipe the data on connected laptop, Syncplicity also provides a way to wipe a user account for computers and mobile devices. The service uses audited security encryption technologies such as SOC 1 and standards such as the Department of Defense’s DoD 5220.22. Finally, the admin console is more robust than some; accounts can be pre-configured for access to specific files and folders for employee groups.
8. Cubby: The name might not make you think of an industrial strength cloud service, but Cubby-from the same company that makes the LogMeIn remote access tool-is a sure step above Dropbox. All user files are protected using SSL encryption; each user gets a separate encryption key. (In a few weeks, Cubby will release a beta for an optional high-security mode that will store encryption keys off-line so that only the user has access to the key.) During a data transfer between users, encryption keys form a tight bond between the two endpoints.
One interesting differentiator for Cubby is that information is stored in a data center owned and operated by LogMeIn, not Amazon or another third party. Like LogMeIn itself, Cubby works on the desktop and through a mobile app in addition to working through any browser.
9. Huddle: Huddle is more than just a place to keep files. The new service, designed for the enterprise, is more like a collaborative environment. This is also one of the strengths of the service in terms of security; repositories are organized according to workgroups and for those inside and outside the company.
Because the service is not designed to be a cloud storage service, the system tracks all documents and transfers. Admins can look up the rights to any document, check who has ownership and see how the document has been distributed in the organization. They can even see a history of comments on documents. Like LogMeIn, Huddle uses a company-owned data center that is ISO 27001 certified. The company conducts regular penetration testing and use enterprise-class firewalls.
10. Egnyte: Like most of the cloud storage systems listed here, Egnyte includes the usual admin console and file encryption options, but several unique feature make this cloud storage portal worth considering. Admins can assign permissions on a more granular level-down to sub-folders within sub-folders.
While other storage tools provide desktop and mobile apps, Egnyte also provides a way to create a mapped network drive or access from FTP. There’s also a way to generate an audit report showing permission and encryption levels. An enterprise version provides up to 3TB of storage and an unlimited number of users, while a unique “server sync” feature creates a sync between a server and the cloud.
11. ShareFile: Another smart alternative to Dropbox, ShareFile (now owned by Citrix) is a stark departure from consumer-level features. IT admins can use a unique “poison pill” option that sets an expiration point for any file. They can also perform a remote data wipe for any computer or mobile device.
12. TeamDrive: TeamDrive is a more secure version of online file sharing services and it uses AES-256 algorithm for encryption plus the public key feature makes it totally reliable and one of the best Dropbox alternative. It is ideal for Private users, small companies, corporations and supports all the platforms like  iOS, Linux and Windows. You also get a a free storage advantage over Dropbox with it’s 10 GB free online storage space. In addition you can also choose between hosted services, your own server or pre-installed TeamDrive cloud.
13. Cloudme: The feature that distinguishes this DropBox alternative from other listed so far is the creative Web Desktop, that allows you to have your own Desktop in the Cloud. You can then customize and increase productivity of your own Web OS by installing apps and create a working environment. This also enables you to to directly listen to your own music, look at images and edit photos. The Cloudme.com supports all the platforms including iOS and Android which means you can also directly Back-up and share information from you smartphones and mobile devices. The email support, MSN, ICQ, Google Talk, Yahoo and AIM integration makes this best Dropbox alternatives which is further accentuated with its free 3GB stirage space.
14. CX: A more social and interactive way to share files using cloud service, the Cx.com provides a free 10GB storage and a neat interface. You can Sync your files to just about every platform including  iOS,Mac, Windows, Kindle and Android but that’s not what differentiates it from the rest of DropBox alternatives. The ability to comment on shared files, add friends, create groups, and then share your files publicly or privately is what makes this one of the best DropBox Alternatives.
Reference : Here
Thanks :), Keep Coding:)

iDev: Getting Started with iOS 7 (Design Icon)

Image

At the keynote Apple event on September 10, 2013, the company not only unveiled their newest iPhones, but they formally announced iOS 7, the latest version of the operating system which drives all Apple mobile devices. It’s scheduled to roll out to the public on Wednesday, September 18 (tomorrow!) and it brings with it a few things that Corona developers should be aware of.

First and foremost, your existing iOS 6.1 and earlier apps should run without any problems. There are, however, a few things that your Corona-built apps must have to behave properly on iOS 7.

App Icons

In iOS 7, Apple has added a few new icon sizes, effectively increased by a few pixels in each of the core usage cases. Of course, you should still support iOS 6.1 and earlier, so the previous icon sizes remain valid and necessary. The questions that arise in regards to iOS icons include:

  • What size(s) are the required icon image files?
  • How must I name these icon image files?
  • Does each icon image file need to be named exactly and specifically?
  • Do they have to be listed in the build.settings file, and if so, where?

Let’s answer these questions now. Since iOS 3.2, Apple has used a plist in the bundle information calledCFBundleIconFiles. This is an array of strings equating to the names of the icons used in your app. For the most part, the naming scheme is fairly flexible, which creates some confusion about exactly how the icons should be named.

To keep this simple for Corona developers, you need to add 4 new required icons and 2 new  recommendedicons, for a total of 6 new image files. Remember to place these files inside your core project folder, not inside a subfolder.

Icon Purpose Recommended Name Size Required
iPhone/iPod Icon-60.png 60×60 Yes
iPhone/iPod (Retina) Icon-60@2x.png 120×120 Yes
iPad 1/2, iPad Mini Icon-76.png 76×76 Yes
iPad 3/4 (Retina) Icon-76@2x.png 152×152 Yes
Spotlight/Settings Icon-Small-40.png 40×40 Recommended
Spotlight/Settings (Retina) Icon-Small-40@2x.png 80×80 Recommended

To minimize changes in your code, we recommend keeping the existing icon file names that you use today, then simply add these new ones to the mix. Your new build.settings file would then contain:

settings =
{
   iphone =
   {
      plist =
      {
         CFBundleIconFiles = {
            "Icon.png",
            "Icon@2x.png",
            "Icon-Small-40.png",
            "Icon-Small-40@2x.png",
            "Icon-60.png",
            "Icon-60@2x.png",
            "Icon-72.png",
            "Icon-72@2x.png",
            "Icon-76.png",
            "Icon-76@2x.png",
            "Icon-Small-50.png",
            "Icon-Small-50@2x.png",
            "Icon-Small.png",
            "Icon-Small@2x.png"
         },
      }
   },
}

Please refer to our Project Build Settings guide for a complete list of the required icon sizes. Many of you may not be including the smaller settings/spotlight search icons.

NOTE: Although this is an iOS-related post, we’ve also added a quick Android update as of Daily Build #1206. Specifically, the new Android xxhdpi.png icon is now supported in Corona, so you should include it if you’re publishing to Android. This new icon size is 144×144 pixels in size — conveniently the same as the iOS Icon-72@2x.png file.

Styling Apps For iOS 7

Perhaps the biggest change in iOS 7 is a completely new look and feel. Apple has “flattened” the interface considerably, steering away from that “real world” look known as skeuomorphic. In other words, buttons don’t need to look like physical buttons to be functional. If you’re a fan of skeuomorphic designs, like a compass app that looks like an actual compass, iOS 7 may not appeal to you visually. However, for those who hate skeuomorphic design (a calendar app doesn’t really need a “spiral binding” after all), Apple has sided with the “less UI is more” team.

In the world of games, this is probably going to have very little impact on your development. However, in the business and utility world, there is a strong, industry-wide movement to flatten designs and move away from skeuomorphism.

For developers using Corona’s current widget library, set to the default iOS theme, you know that the widgets resemble the native widgets in iOS 6.1 and earlier. Now, for those who are eager to begin styling their apps for iOS 7, we have added an iOS 7 widget theme as of Daily Build #1206. To use it, simply include this line of code after you require the widget library:

local widget = require( "widget" )
widget.setTheme( "widget_theme_ios7" )

One current exception is the picker wheel widget. Native iOS 7 uses some 2.5D visualizations wherein the top and bottom of the wheel is skewed slightly to provide a sense of depth. We’ll add this to the Corona picker wheel widget when Graphics 2.0 is officially released, but for now your picker wheels will appear flat.

Fonts

The new font for iOS 7, in most cases, is Helvetica Neue Light. If you want to build apps that match iOS 7 in typography, you’ll probably want to use this font as well, for example:

local myButton = widget.newButton{
   left = display.contentCenterX - 100,
   top = 360,
   label = "Send Message",
   width = 200, 
   height = 48,
   font = "HelveticaNeue-Light",
   onEvent = onButtonEvent
}

Fortunately, this font is included in both iOS 6 and iOS 7, as well as on OS X. If you want a bolder version, just use“HelveticaNeue”. If you need a template for iOS 7 imagery, an Adobe Photoshop PSD of graphical elements is located here — just make sure that you read and understand the legal usage terms that accompany this download. Finally, a good article that describes Apple’s requirements and recommendations is located here.

Building for iOS 7

Public Build #1202 can only build for iOS 6.1 and earlier, but Daily Builds after that have an additional dialog option called iOS SDK which we include at any time there are two versions of the iOS SDK to build for. As of Daily Build #1203, choosing iOS 7 will build against the “iOS 7 Gold Master seed.” This means that you can submit builds to Apple using this option. Daily Build #1203 – #1206 lists this as a “Beta” but it is the GM seed, not one of the Beta versions.

While we are using the iOS 7 GM seed SDK, it’s still in testing mode. We are working out the last few details before we enable app submission against the iOS 7 target. However, you should be testing against it, and there are a few other software requirements before you can do so:

  1. Building for iOS 7 requires XCode 5. Apple made XCode 5 available today in the Mac App Store as a free download. This will install over top of your existing Xcode 4.x installation.
  2. You must be running OS-X Mountain Lion (10.8) or later to run XCode 5.

If you have been running the XCode 5 developer preview, but you need to revert back to building with 4.x or if you want to use the XCode 5 GM Seed, you’ll need to specify so using the following command line (Terminal) command:

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

In Summary

iOS 7 brings a fresh new look to iOS, while at the same time introducing many new features and UI improvements.

Reference: Here

Thanks 🙂 Keep Coding 🙂

iDev : Experiments with precise timing in iOS

Experiments with precise timing in iOS

iOS is by no means a realtime operating system, but I’m aware that NSTimer and NSObject’sperformSelector:withObject:afterDelay: mechanism aren’t particularly accurate, and I was curious to see whether I could do better.

Hands up, backing away

Disclaimer: I am not at all an expert in realtime programming, or Mach, or iOS-device optimisation, so this is pretty much a fumble in the dark. I won’t be at all offended if anyone wishes to shoot me down and offer a more sensible solution — in fact, please do! Until then, watch as I stumble on…

Also note that there are often ways to eliminate the need for precise timing of this nature, by architecting code appropriately — when it comes to audio, for example, CoreAudio provides a very accurate time base in render callbacks. For things like metronomes or audio synthesizers, it’s always better to establish a starting time, and use the difference between the current time and the starting time in order to determine state, rather than using a timer to advance the state. Still, sometimes, you just need a timer…

What the blazes?

So, I’m working on an update to Loopy, which uses a shared clock object to synchronise tracks and a variety of events (like user interface updates or timed track manipulations). A tester noted that the mute/unmute quantisation feature that I’ve recently implemented, which will mute or unmute a loop at its starting point (rather than whenever you tap it), tends to overshoot a little, resulting in a small part of the beginning of the loop being audible.

Of course, there are other solutions to this particular problem (like stopping or starting playback from the audio render callback, and using Core Audio’s timestamps for exact timing), but I use timers in other places outside Core Audio’s domain, which makes Core Audio’s timing mechanism unavailable, and I wanted to see how accurate I could get the timing.

Our friend, mach_wait_until

I read in several places mention of the Mach API utility mach_wait_until (frommach/mach_time.h), which is very low-level and supposedly fairly accurate. So, based on that lead, I put together an Objective-C singleton class that launches a high-priority thread, and uses said thread to schedule events.

An NSArray of events are maintained, and a scheduleAction:target:inTimeInterval: routine creates and adds events to this array, then pokes the thread.

The thread grabs the next event in sequence, then uses mach_wait_until to sleep until the time of the next event arrives, then performs the specified action on the target. It’s kinda a DIY NSRunLoop.

Here’s a comparison between this technique, and just usingperformSelector:withObject:afterDelay: (which schedules a timer on the NSRunLoop), observed while performing various scheduled events within Loopy running on my iPhone 4 with the debugger, and derived by comparing the time of event execution with the event’s scheduled time:

Mechanism Average discrepancy Minimum discrepancy Maximum discrepancy
NSRunLoop 16.9ms 0.25ms 153.7ms
TPPreciseTimer 5.5ms 0.033ms 72.0ms

That was attempt number 1: This seems to give us about 11.4ms better accuracy on average (three times more accurate).

Not bad, but it turns out mach_wait_until isn’t really that accurate, particularly if there’s a bunch of other stuff going on in other threads.

Spinning, for fun and profit

For my second attempt, the thread performs a mach_wait_until until just before the event is due, then performs a spin lock until the time arrives, using mach_absolute_time to compare the current time with the target time.

This gave further improved results — here’s that table again, but with the new scheme added, with a few different spin lock times:

Mechanism Average discrepancy Minimum discrepancy Maximum discrepancy
NSRunLoop 16.9ms 0.25ms 153.7ms
TPPreciseTimer (original) 5.5ms 0.033ms 72.0ms
TPPreciseTimer (10ms spinlock) 6.0ms 0.002ms 76.5ms
TPPreciseTimer (100ms spinlock) 3.7ms 0.002ms 44.8ms
TPPreciseTimer (200ms spinlock) 2.91ms 0.002ms 74.1ms

It appears that the more stuff there is going on in other threads, the more likely themach_absolute_time call is to overshoot. So, the more time spent in the spin lock, the more leeway mach_absolute_time has to wait too long. Of course, that’s at the cost of making the CPU twiddle its thumbs for the duration.

Better than a punch in the knee

The results weren’t quite as fantastic as I’d hoped — still within the same order of magnitude, that’s for sure — but the average case for the 200ms spinlock approach is 14ms, or 5.8 times, more accurate than the traditional approach, and the minimum case is dramatically better.

You know, I think if I was aware of the results in advance, I might not bother, but I’ll stick with my hard-won 14ms now that I’m here (that’s 617 audio samples, I’ll have you know).

If anyone’s curious about the implementation (or wants to take a stab at doing better), here it is, along with a wildly simplistic commandline test app: TPPreciseTimer.zip

Now to get back to some real work.

Addendum: GCD follow-up

Chris in the comments below suggested trying a GCD-based approach, using dispatch_after. Curious, I rigged it up, and these are the stats, collected the same way as above, added to the prior table:

Mechanism Average discrepancy Minimum discrepancy Maximum discrepancy
NSRunLoop 16.9ms 0.25ms 153.7ms
TPPreciseTimer (original) 5.5ms 0.033ms 72.0ms
TPPreciseTimer (10ms spinlock) 6.0ms 0.002ms 76.5ms
TPPreciseTimer (100ms spinlock) 3.7ms 0.002ms 44.8ms
TPPreciseTimer (200ms spinlock) 2.91ms 0.002ms 74.1ms
dispatch_after (main queue) 14.8ms 0.16ms 161.2ms
dispatch_after (dedicated queue) 19.2ms 0.1ms 174.9ms
dispatch_after (dedicated queue + 100ms spinlock) 22.4ms 0.002ms 306.8ms

So, they appear pretty much the same as the NSRunLoop stats.

Reference : Here

Thanks, Keep Coding:)