Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/choste/public_html/press/wp-content/themes/suffusion/functions/media.php on line 669

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/choste/public_html/press/wp-content/themes/suffusion/functions/media.php on line 674

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/choste/public_html/press/wp-content/themes/suffusion/functions/media.php on line 687

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/choste/public_html/press/wp-content/themes/suffusion/functions/media.php on line 692

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/choste/public_html/press/wp-content/themes/suffusion/functions/media.php on line 697
Taylor Peet » UnRulyRecursion

Taylor Peet

Feb 142014
 

Howdy hackers,

If you play around with Android, here’s a quick tip I found today:

Parcelable vs Serializable

april 18, 2013

When starting on Android, we all learn that we cannot just pass object references to activities and fragments, we have to put those in an Intent / Bundle.

Looking at the api, we realize that we have two options, we can either make our objects Parcelable orSerializable. As Java developers, we already know of the Serializable mechanism, so why bother with Parcelable?

To answer this, lets take a look at both approaches.

Serializable, the Master of Simplicity

1
2
3
4
5
6
7
8
9
10
11
12
// access modifiers, accessors and constructors omitted for brevity
public class SerializableDeveloper implements Serializable
    String name;
    int yearsOfExperience;
    List<Skill> skillSet;
    float favoriteFloat;
    static class Skill implements Serializable {
        String name;
        boolean programmingRelated;
    }
}

The beauty of serializable is that you only need to implement the Serializable interface on a class and its children. It is a marker interface, meaning that there is no method to implement, Java will simply do its best effort to serialize it efficiently.

The problem with this approach is that reflection is used and it is a slow process. This mechanism also tends to create a lot of temporary objects and cause quite a bit of garbage collection.

Parcelable, the Speed King

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// access modifiers, accessors and regular constructors ommited for brevity
class ParcelableDeveloper implements Parcelable {
    String name;
    int yearsOfExperience;
    List<Skill> skillSet;
    float favoriteFloat;
    ParcelableDeveloper(Parcel in) {
        this.name = in.readString();
        this.yearsOfExperience = in.readInt();
        this.skillSet = new ArrayList<Skill>();
        in.readTypedList(skillSet, Skill.CREATOR);
        this.favoriteFloat = in.readFloat();
    }
    void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(yearsOfExperience);
        dest.writeTypedList(skillSet);
        dest.writeFloat(favoriteFloat);
    }
    int describeContents() {
        return 0;
    }
    static final Parcelable.Creator<ParcelableDeveloper> CREATOR
            = new Parcelable.Creator<ParcelableDeveloper>() {
        ParcelableDeveloper createFromParcel(Parcel in) {
            return new ParcelableDeveloper(in);
        }
        ParcelableDeveloper[] newArray(int size) {
            return new ParcelableDeveloper[size];
        }
    };
    static class Skill implements Parcelable {
        String name;
        boolean programmingRelated;
        Skill(Parcel in) {
            this.name = in.readString();
            this.programmingRelated = (in.readInt() == 1);
        }
        @Override
        void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeInt(programmingRelated ? 1 : 0);
        }
        static final Parcelable.Creator<Skill> CREATOR
            = new Parcelable.Creator<Skill>() {
            Skill createFromParcel(Parcel in) {
                return new Skill(in);
            }
            Skill[] newArray(int size) {
                return new Skill[size];
            }
        };
        @Override
        int describeContents() {
            return 0;
        }
    }
}

According to google engineers, this code will run significantly faster. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.

However, it is obvious here that implementing Parcelable is not free. There is a significant amount of boilerplate code and it makes the classes harder to read and maintain.

Speed Tests

Of course, we want to know how much faster Parcelable is.

The methodology

  • Mimic the process of passing object to an activity by putting an object in a bundle and callingBundle#writeToParcel(Parcel, int) and then fetching it back
  • Run this in a loop 1000 times
  • Do an average on 10 separate runs to account for memory allocation, other apps using the cpu, etc
  • The object under test are the SerializableDeveloper and the ParcelableDeveloper shown above
  • Test on multiple devices – android versions
    • LG Nexus 4 – Android 4.2.2
    • Samsung Nexus 10 – Android 4.2.2
    • HTC Desire Z – Android 2.3.3

The results

parcelable vs serializable

Nexus 10

Serializable: 1.0004ms,  Parcelable: 0.0850ms – 10.16x improvement.

Nexus 4

Serializable: 1.8539ms – Parcelable: 0.1824ms – 11.80x improvement.

Desire Z

Serializable: 5.1224ms – Parcelable: 0.2938ms – 17.36x improvement.

There you have it: Parcelable is more than 10x faster than Serializable! It is also interesting to note that even on a Nexus 10, a pretty simple object can take about 1 millisecond to go through a full serialize/deserialize cycle.

The Bottom Line

If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources.

However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it  but  remember that serialization is an expensive operation so keep it to a minimum.

If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to lanscape feel very sluggish.

source: http://www.developerphil.com/parcelable-vs-serializable/
Sep 252013
 

Here is the text of an interesting article on story telling. I’ll be using these concepts to craft arcs for videos in the near future, stay tuned!

-T

source: http://www.dailywritingtips.com/how-to-structure-a-story-the-eight-point-arc/

How to Structure A Story: The Eight-Point Arc

by Ali Hale

One of my favourite “how to write” books is Nigel Watts’ Writing A Novel and Getting Published.

My battered, torn and heavily-pencil-marked copy is a testament to how useful I’ve found it over the years. Although the cover appears to be on the verge of falling off altogether, I’ve risked opening the book once more to bring you Watts’ very useful “Eight-Point Story Arc” – a fool-proof, fail-safe and time-honoured way to structure a story.

(Even if you’re a short story writer or flash fiction writer rather than a novelist, this structure still applies, so don’t be put off by the title of Watts’ book.)

The eight points which Watts lists are, in order:

  1. Stasis
  2. Trigger
  3. The quest
  4. Surprise
  5. Critical choice
  6. Climax
  7. Reversal
  8. Resolution

He explains that every classic plot passes through these stages and that he doesn’t tend to use them to plan a story, but instead uses the points during the writing process:

I find [the eight-point arc] most useful as a checklist against which to measure a work in progress. If I sense a story is going wrong, I see if I’ve unwittingly missed out a stage of the eight-point arc. It may not guarantee you write a brilliant story, but it will help you avoid some of the pitfalls of a brilliant idea gone wrong.

So, what do the eight points mean?

Stasis

This is the “every day life” in which the story is set. Think of Cinderella sweeping the ashes, Jack (of Beanstalk fame) living in poverty with his mum and a cow, or Harry Potter living with the Dursley’s.

Trigger

Something beyond the control of the protagonist (hero/heroine) is the trigger which sparks off the story. A fairy godmother appears, someone pays in magic beans not gold, a mysterious letter arrives … you get the picture.

The quest

The trigger results in a quest – an unpleasant trigger (e.g. a protagonist losing his job) might involve a quest to return to the status quo; a pleasant trigger (e.g. finding a treasure map) means a quest to maintain or increase the new pleasant state.

Surprise

This stage involves not one but several elements, and takes up most of the middle part of the story. “Surprise” includes pleasant events, but more often means obstacles, complications, conflict and trouble for the protagonist.

Watts emphasises that surprises shouldn’t be too random or too predictable – they need to be unexpected, but plausible. The reader has to think “I should have seen that coming!”

Critical choice

At some stage, your protagonist needs to make a crucial decision; a critical choice. This is often when we find out exactly who a character is, as real personalities are revealed at moments of high stress. Watts stresses that this has to be a decision by the character to take a particular path – not just something that happens by chance.

In many classic stories, the “critical choice” involves choosing between a good, but hard, path and a bad, but easy, one.

In tragedies, the unhappy ending often stems from a character making the wrong choice at this point – Romeo poisoning himself on seeing Juliet supposedly dead, for example.

Climax

The critical choice(s) made by your protagonist need to result in the climax, the highest peak of tension, in your story.

For some stories, this could be the firing squad levelling their guns to shoot, a battle commencing, a high-speed chase or something equally dramatic. In other stories, the climax could be a huge argument between a husband and wife, or a playground fight between children, or Cinderella and the Ugly Sisters trying on the glass slipper.

Reversal

The reversal should be the consequence of the critical choice and the climax, and it should change the status of the characters – especially your protagonist. For example, a downtrodden wife might leave her husband after a row; a bullied child might stand up for a fellow victim and realise that the bully no longer has any power over him; Cinderella might be recognised by the prince.

Your story reversals should be inevitable and probable. Nothing should happen for no reason, changes in status should not fall out of the sky. The story should unfold as life unfolds: relentlessly, implacably, and plausibly.

Resolution

The resolution is a return to a fresh stasis – one where the characters should be changed, wiser and enlightened, but where the story being told is complete.

(You can always start off a new story, a sequel, with another trigger…)

I’ve only covered Watts’ eight-point arc in brief here. In the book, he gives several examples of how the eight-point arc applies to various stories. He also explains how a longer story (such as a novel) should include arcs-within-arcs – subplots and scenes where the same eight-point structure is followed, but at a more minor level than for the arc of the entire story.

You can buy Writing a Novel from Amazon.com – and I highly recommend that you do, as it’s an excellent book for any writer of fiction, and deals with all aspects of the craft (not just eight-point arcs!)