Why does PhoneGap seem faster than Titanium? - iphone

I'm trying to measure the execution perfomance of a few cross-platform solutions, among which are: Titanium and PhoneGap.
So here's an example of the Titanium version of my performance tester, it's very simple, but I'm just trying to get a feeling of how fast my code gets executed:
var looplength;
var start1;
var start2;
var end1;
var end2;
var duration1;
var duration2;
var diff;
var diffpiter;
var power;
var info;
for (power = 0; power < 24; power++) {
looplength = Math.pow(2, power);
start1 = new Date().getTime();
for (iterator = 0; iterator < looplength; iterator++) {a=iterator;b=iterator;}
end1 = new Date().getTime();
start2 = new Date().getTime();
for (iterator = 0; iterator < looplength; iterator++) {a=iterator;}
end2 = new Date().getTime();
duration1 = end1 - start1;
duration2 = end2 - start2;
diff = duration1 - duration2;
diffpiter = diff / looplength;
info={title:'2^' + power + ' ' + diffpiter};
tableView.appendRow(Ti.UI.createTableViewRow(info),{animated:true});
}
The PhoneGap version is the same except for the last two lines which get replaced
document.write('2^' + power + ' ' + diffpiter + '<br />');
Both are executed on an iPhone 4S. I've run the test numerous times, to eliminate errors.
How in the name of all that is holy can the Titanium version measure ~0.0009 milliseconds per iteration while the PhoneGap version measures ~0.0002 milliseconds per iteration?
Titanium is supposed to compile my javascript code so I expect it to be faster. In this case however it's at least 4 times slower! I'm not an expert on performance testing, but the test I designed should be at least remotely accurate...
Thank you for any tips you can give me.

Titanium doesn't convert javascript code to objective-c. Titanium simply uses a javascript to objective-c bridge to communicate with objective-c iOS framework (most importantly User interface objects). More appropriate comparison would be to code titanium's User Interface element (button, label, window, view ), manipulate them and use html,css,image buttons in phonegap.
Phonegap also uses a bridge of it's own and if you know java or objective-c you can make plugins to use native User Interface elements and other Native features of iOS or Android.
http://zsprawl.com/iOS/2012/05/navigation-bar-with-nativecontrols-in-cordova/

This is basic JavaScript, and not all JavaScript is compiled to native code. Basically when you use the Titanium API, that will be converted to Objective-C or Java code. But to be flexible and dynamic there is a JavaScript interpreter compiled with the App, and that basically runs the JavaScript you have written.
This makes the App slower. But testing it purely on these things is useless. If you want to do a full suit of testing you need to use the Titanium API too, and compare that to the PhoneGap one.
What you'll notice, as Phonegap does not compile to native code, it will feel different, and visually Titanium will behave faster.

Oh man, I don't want to start a flame war but I will put in my two cents. First, full disclosure: I'm a contributor to PhoneGap and I've never used Titanium. However I am answering from 15 years of development experience.
I've never found tools that convert code from one language to another to be particularly efficient. Yes, native code should run faster than JavaScript code but I'm willing to bet there are inefficiencies introduced during the translation phase.
Again this is just from past experience using tools that compile one language into another it is not a knock on Titanium as that is a great framework.

In your TItanium code, your last line is creating UI objects - this is making a call to Objective-C to create a UITableViewRow and an animation object and then appending it to a UITableView - you are doing 3 operations. I'd be pretty confident that this is what is taking the time. The preferred Ti way of doing this would be create an array of title objects, then using setData on the table at the end.
PhoneGap has already created the UIWebView on app load and you are just updating the html in one DOM element so I would expect the UI will be faster.

Related

xamarin.forms read app version from app store / google store

I need to know when i start running my app if a new version exists in Google Play Store / ios app store.
I want to do it by comparing my running app version to the one in the store.
How do i read the app version from the store in run time ?
string url = "http://itunes.apple.com/lookup?bundleId=YourBundelID";
using (var webClient = new System.Net.WebClient())
{
string jsonString = webClient.DownloadString(string.Format(url));
var lookup = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
if (lookup != null
&& lookup.Count >= 1
&& lookup["resultCount"] != null
&& Convert.ToInt32(lookup["resultCount"].ToString()) > 0)
{
var results = JsonConvert.DeserializeObject<List<object>>(lookup[#"results"].ToString());
if (results != null && results.Count > 0)
{
var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(results[0].ToString());
appStoreAppVersion = values.ContainsKey("version") ? values["version"].ToString() : string.Empty;
}
}
}
return appStoreAppVersion;
If there's no easy way to achieve this, build your own easy way ;-)
For example: with every upload to the app store you upload the current version info to an online resource you can access from your app. All you need is a webserver where you can either run your own API or just upload a file containing the version info.
Maybe not a solution you awaited, but it's definitely going to work.
There is no (easy) way to do that. The best way to do it would be to access the app details through some API but I don't think they make that (publicly) available. So the other way would probably to scrape the according sites, but I'm pretty sure they will be unhappy with that method and maybe their license even forbids it.
Also, mostly that kind of solutions is due to poor design. The user has to be able to use the old version for the length of days, although you should strive for fast adoption of your new version.
But just keep in mind while designing your backend etc. that you have to be backwards compatible.
Lastly, as a user I really hate those kind of messages. Depending on what kind of app it is I just want to use it then, not be forced to update, so go out, update, and try to do the thing I opened it for in the first place.

Is it safe to use performance.now() to measure page load time in GWT?

I need to measure the time required for my GWT application to load.
I can't use GWT Lightweight Metrics because of external framework usage (I can't modify bootstrap html page).
Navigation Timing doesn't work for GWT because initialization of GWT widgets doesn't depend on window.load.
So, is it safe to use performance.now() method to get the time from PerformanceTiming.navigationStart event?
Specs says that:
The now() method must return a DOMHighResTimeStamp representing the time in milliseconds from the time origin to the occurrence of the call to the Performance.now method.
I can't understand time origin definition. Does it always return the time from the moment when the user requests a page with my application?
I haven't found answer to my original question. It seems like performance.now() isn't suitable for measuring page loading time.
I have used explicit approach described here.
The code is something like this.
public static long getTimeFromPageLoadStart() {
long navigationStart = (long) getNavigationStart();
return new Date().getTime() - navigationStart;
}
private static native double getNavigationStart()/*-{
return performance.timing.navigationStart;
}-*/;

Controlling light using midi inputs

I currently am using Max/MSP to create an interactive system between lights and sound.
I am using Philips hue lighting which I have hooked up to Max/MSP and now I am wanting to trigger an increase in brightness/saturation on the input of a note from a Midi instrument. Does anyone have any ideas how this might be accomplished?
I have built this.
I used the shell object. And then feed an array of parameters into it via a javascipt file with the HUE API. There is a lag time of 1/6 of a second between commands.
Javascript file:
inlets=1;
outlets=1;
var bridge="192.168.0.100";
var hash="newdeveloper";
var bulb= 1;
var brt= 200;
var satn= 250;
var hcolor= 10000;
var bulb=1;
function list(bulb,hcolor,brt,satn,tran) {
execute('PUT','http://'+bridge+'/api/'+hash+'/lights/'+bulb+'/state', '"{\\\"on\\\":true,\\\"hue\\\":'+hcolor+', \\\"bri\\\":'+brt+',\\\"sat\\\":'+satn+',\\\"transitiontime\\\":'+tran+'}"');
}
function execute($method,$url,$message){
outlet(0,"curl --request",$method,"--data",$message,$url);
}
To control Philips Hue you need to issue calls to a restful http based api, like so: http://www.developers.meethue.com/documentation/core-concepts, using the [jweb] or [maxweb] objects: https://cycling74.com/forums/topic/making-rest-call-from-max-6-and-saving-the-return/
Generally however, to control lights you use DMX, the standard protocol for professional lighting control. Here is a somewhat lengthy post on the topic: https://cycling74.com/forums/topic/controlling-video-and-lighting-with-max/, scroll down to my post from APRIL 11, 2014 | 3:42 AM.
To change the bri/sat of your lights is explained in the following link (Registration/Login required)
http://www.developers.meethue.com/documentation/lights-api#16_set_light_state
You will need to know the IP Address of your hue hue bridge which is explained here: http://www.developers.meethue.com/documentation/getting-started and a valid username.
Also bear in mind the performance limitations. As a general rule you can send up to 10 lightstate commands per second. I would recommend having a 100ms gap between each one, to prevent flooding the bridge (and losing commands).
Are you interested in finding out details of who to map this data from a MIDI input to the phillips HUE lights within max? or are you already familiar with Max.
Using Tommy b's javascript (which you could put into a js object), You could for example scale the MIDI messages you want to use using midiin and borax objects and map them to the outputs you want using the scale object. Karlheinz Essl's RTC library is a good place to start with algorithmic composition if you want to transform the data at all http://www.essl.at/software.html
+1 for DMX light control via Max. There are lots of good max-to-dmx tutorials and USB-DMX hardware is getting pretty cheap. However, as someone who previously believed in dragging a bunch of computer equipment on stage just to control a light or two with an instrument, I'd recommend researching and purchasing a simple one channel "color organ" circuit kit (e.g., Velleman MK 110). Controlling a 120/240V light bulb via audio is easier than you might think; a computer for this type of application is usually overkill. Keep it simple and good luck!

JPEG encoder super slow, how to Optimize it?

I'm building an App with actionscript 3.0 in my Flash builder. This is a followup question this question.
I need to upload the bytearray to my server, but the function i use to convert the bitmapdata to a ByteArray is super slow, so slow it freezes up my mobile device. My code is as follows:
var jpgenc:JPEGEncoder = new JPEGEncoder(50);
trace('encode');
//encode the bitmapdata object and keep the encoded ByteArray
var imgByteArray:ByteArray = jpgenc.encode(bitmap);
temp2 = File.applicationStorageDirectory.resolvePath("snapshot.jpg");
var fs:FileStream = new FileStream();
trace('fs');
try{
//open file in write mode
fs.open(temp2,FileMode.WRITE);
//write bytes from the byte array
fs.writeBytes(imgByteArray);
//close the file
fs.close();
}catch(e:Error){
Is there a different way to convert it to a byteArray? Is there a better way?
Try to use blooddy library: http://www.blooddy.by . But i didn't test it on mobile devices. Comment if you will have success.
Use BitmapData.encode(), it's faster by orders of magnitude on mobile http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#encode%28%29
You should try to find a JPEG encoder that is capable of encoding asynchronously. That way the app can still be used while the image is being compressed. I haven't tried any of the libraries, but this one looks promising:
http://segfaultlabs.com/devlogs/alchemy-asynchronous-jpeg-encoding-2
It uses Alchemy, which should make it faster than the JPEGEncoder from as3corelib (which I guess is the one you're using at the moment.)
A native JPEG encoder is ideal, asynchronous would be good, but possibly still slow (just not blocking). Another option:
var pixels:ByteArray = bitmapData.getPixels(bitmapData.rect);
pixels.compress();
I'm not sure of native performance, and performance definitely depends on what kind of images you have.
The answer from Ilya was what did it for me. I downloaded the library and there is an example of how to use it inside. I have been working on getting the CameraUI in flashbuilder to take a picture, encode / compress it, then send it over via a web service to my server (the data was sent as a compressed byte array). I did this:
by.blooddy.crypto.image.JPEGEncoder.encode( bmp, 30 );
Where bmp is my bitmap data. The encode took under 3 seconds and was easily able to fit into my flow of control synchronously. I tried async methods but they ultimately took a really long time and were difficult to track for things like when a user moved from cell service to wifi or from tower to tower while an upload was going on.
Comment here if you need more details.

tlab Audio conversion

I recorded my voice in Matlab. Now i want to convert that audio in to strings i-e; written sentences in Matlab. Is there a way to convert audio in to text.
I'm pretty sure MATLAB does not have native speech-to-text functionality.
A quick Google search turned up at least one project integrating speech-to-text into MATLAB.
http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
Some other software that can translate recorded speech into text are Microsoft's SAPI (built into Windows Vista and Windows 7, and available as a download for Windows XP), and CMU's Sphinx project. Nuance Dragon Naturally Speaking is an option, but it is comparatively expensive. It's not obvious to me how these could be integrated into MATLAB though.
You can achieve somewhat limit mileage using the Builtin Windows Speech API. It depends on your operating system etc. and you need to follow similar principles from the API documentation:
http://msdn.microsoft.com/en-us/library/ms723627(v=vs.85).aspx
Using MATLAB's activeX server (
http://www.mathworks.co.uk/help/matlab/ref/actxserver.html)
You need the first declare a speech recogniser engine
RC = actxserver('SAPI.SpSharedRecoContext'); %connect to speech engine
And then setup various call back functions for each state of the recogniser:
RC.registerevent({'Recognition' #CallbackFunction; 'Hypothesis' #CallbackFunction; 'FalseRecognition' #CallbackFunction})
The contents of the callback function should be along these lines:
function word = CallbackFunction(varargin)
global word
result = varargin{length(varargin)-2};
word = result.Phraseinfo.GetText;
end
Then finally switch the recogniser on:
RC.Recognizer.State = 'SRSActive';
You would need to reference the documentation for which callback functions are called and when.
You will need to also setup a grammar dictionary to get meaningful results. As the engine will be attempting to recognise any word otherwise.