Showing posts with label copy. Show all posts
Showing posts with label copy. Show all posts

Thursday, 19 July 2018

C++ : Copy Constructor versus Ignorance

I've spent a bit of time reviewing someone else's code recently and I've come to an impasse with them, so they have a lot of code which will take some standard container, and the code doesn't just initialise the local copy from the passed in reference... No it'll iterate over the list of elements adding them to the class version.

I have picked fault with this, it's not RAII, it looks ugly and if you're threading you can create your class instance and the member is empty or in a partially filled state before the loop within the constructor is finished... I highlight this in red below...

My solution?  Just initialise the member from the reference - see the green highlight in the code below.

My results from the timing below?



These times are "microseconds" so tiny... But with just constructing from the existing reference we always get a lower time, quicker code...


Running this test 30,000 times, trying it in different orders and with maps of upto 1000 elements I had a rough average increase of 60% speed by using the copy constructor of std::map rather than reallocating new memory for each pre-existing element.

I wanted therefore to understand the reasoning behind the original code, was there some reason to perform a clone (alloc and assign new instance) operation for each pair in the map being passed in?  Looking at the code there was no apparent reason, so I spoke to the developer, asking why they had performed the construction in this manner... Their reply...

"That's how you initialise a container"

You can load up a container in this manner, but you already have the contents of the map, you're just copying it... "Why don't you use the copy constructor?"... I asked...

"I didn't write one"

Hmm, "You don't, the compiler generates it for you, std::map has its own copy constructor".  Use the copy constructor folks, trust me.


#include <map>
#include <chrono>
#include <string>
#include <iostream>


using Mapping = std::map<int, int>;

class A
{
private:
Mapping m_TheMapping;

public:

A() = delete;
A(const A&) = delete;
void operator=(const A&) = delete;

A(const Mapping& p_Mapping)
:
m_TheMapping(p_Mapping)
{
}

A(const Mapping& p_Mapping, const bool& p_Other)
:
m_TheMapping()
{
for (auto i : p_Mapping)
{
m_TheMapping.emplace(i);
}
}

inline Mapping& Mapping()
{
return m_TheMapping;
}
};


int main()
{
Mapping l_m
{
{ 0, 0 },
{ 1, 1 },
{ 2, 2 },
{ 3, 3 }
};

auto l_time(std::chrono::high_resolution_clock::now());
// Copy Construction
A l_map(l_m);
auto l_timeB(std::chrono::high_resolution_clock::now());

auto l_Dur(l_timeB - l_time);
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(l_Dur).count() << std::endl;


auto l_time2(std::chrono::high_resolution_clock::now());
// Clone Construction
A l_map2(l_m, true);
auto l_time2B(std::chrono::high_resolution_clock::now());

auto l_DurB(l_time2B - l_time2);
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(l_DurB).count() << std::endl;
}

Wednesday, 13 May 2015

BBC Copy Proofing Still Bad

For a while I've taken to pointing out faults in stories on the BBC, and I'm still flabbergasted when mistakes are made, not least silly things like this:

Taps his feet... Not Foots....

It's so annoying, because this article isn't rushed, isn't really under duress, and unlike my own blog it's an official news outlet, this is written by a professional reporter and ergo professional writer.  Gah.

Thursday, 5 February 2015

4D Cinema

I've just been watching a report from the BBC's Jo Black about 4DX cinema, and it being invented in Korea... 4D cinema was not invented in Korea a few years ago, it was around at the American Adventure here in the UK in the 1980's and 1990's!

It was called the "Motion Master Theatre"...


You can see the BBC report here:


But you can find pictures of the 4D cinema at the American Adventure here:



The projector here:


The motion systems were controlled by a DEC PDP11 mini-computer of mid 1980's production vintage, I've seen pictures of this system, but never saw it in person.

You can find pictures of the Motion Control room here:

http://www.theamericanadventure.moonfruit.com/#/behind-the-scenes/4521465560

I have of course rode the very uncomfortable seats at the park before it closed.


Tuesday, 5 August 2014

Software - Sharing, Piracy, Subscriptions

I'm going to hedge my bets here and assume the limitation of prosecuting me is well expired, and I'm going to talk about something I did, probably something you did, perhaps something everyone of my generation and inclination did... And that was pirate software.

The difficulty in saying "I pirated software" today is that it holds such a negative connotation, is carries prosecution and it lands you in a lot of trouble; so kids don't do it!

But back in the 1980's we didn't even call it pirating, we shared software, we lent software, and sometimes we bought it of course.  But as a kid partaking of a hobby where you could buy an old audio album for 10p or a new game for £1.99, you bought the album for the cassette and then you copied software over onto it.  The copying wasn't even done on a computer, so primitive were they, no you copied your software on a hifi, with a tape to tape, or deck to deck, cassette recorder...

This was my introduction to software, and how you handled it, unfortunate for the many companies and individuals who didn't get my pocket money, but in the long run this sharing went out and out from everyone and through every computer platform I knew until I think around the year 1999, when I sort of stopped getting involved in software sharing and started to play online games which needed a real copy of the game.

But before that, for generations of machines, we all shared and shared a like, and as opposed to the companies who lost out on revenue I think we need to think about the ethos of shareware - born in the 80's - as well as freeware and the free software foundation, which though gaining ground now was lost on the mass of 80's financial effluent.

People sell things to other people who pay for them, that's how the world goes around, except with software, where the bits and bytes can be facsimiled, copied, duplicated in less than a blink of the eye.

In this special realm of software I believe that still paying for games at the extortionate prices they are is getting rather boring, we went from avoiding paying for £1.99 games, to kids today demanding - and getting - £49.99 games regularly.  It boggles the mind.

Copying or pirating those games does go on, but with the hardware level of entry and risks of prosecution involved it is very much an underground and unspoken topic of conversation.

However, you ask a real enthusiast about pirating older software "back in the day" everyone, and I mean everyone, worth their salt will have a story to tell.

And armed with hindsight, as well as the up-swell of Free to Play online titles (World of Tanks, WarThunder, Heroes and Generals, Unturned, D&D Neverwinter etc etc) you have to argue their production value is as much as any on the shelf for £49.99.  Except they're free, if you so chose you could sit and play for ever for free.  This may not benefit the developer, as you're not paying for content nor paying for the title, but if you did, employing Micro Transactions or subscriptions to attain a bleeding edge, as they do.  Then you are paying less for a game you might potentially play and get more from.

Also, with such transactions you can choose how and where to make them, £19.99 for a head start in a new game you've already gotten into for free is not a bad idea, when you could have been paying that same for a Subscription to World of Warcraft or Eve-Online, and simply not playing enough to warrant the money spent.

So how does this come full circle to the good old days of sharing software?  Well, we used to share by having face to face meet ups, we would physically carry the cassette, or floppy, or even magazine with the code written within from person to person.  With the internet today this is redundant... But should it be so?...

Could there be a place in the world for a face to face software meet & greet?...



P.S. My story of pirating, was with a lad at school who thought he was the bee's knees... 

I remember him arguing with and calling another kid dim because the kids said "CD's turn around in the player", and this "cool kid" strenuously denied they turned... "They don't spin around you retard" he said... Zahid... Richard was right, CD's spin you fuck-tard...

Well, this same Zahid thought himself the Godfather of pirate video, probably because he'd made a copy of beatlejuice and shared it around his mates.  But he came up to me and declared "What do you know about cassettes, how do you stop them being copied over?"... I of course knew, you just remove the plastic tab... but this moron was so above and beyond himself he thought this was special knowledge...

The fact it was printed on every video cassette manual said how much he read the dim wit fuck that he was, but he was happy to try and take the piss out of me for "not knowing", and when I knew he just wandered off mumbling.

It is dick heads like him out there now breeding kids who demand £49.99 video games and he's probably providing them from his dole money, but I still think a trick was lost, if we'd just educated those types to share like we all did... Perhaps today the software landscape would be different...

Tuesday, 6 August 2013

Copy, what's a copy? Oh near enough'll do....

I remember the days when the xerographic process was a mechanical physical operation of light and toner on a drum, so your copies - though they came out slightly worse for wear - were identical to the original, there was no digital tom follery and there was no middle man here.

And perhaps there never should have been, save for the cost of duplication, to minimise costs copiers started to use digital scanning, I noticed this rise about twenty years ago, when the office copier, which we'd lovingly beaten into a pulp for splashing us with toner and smashed the glass on by sitting on it, was finally replaced, and we got a machine noticeably smaller and lighter doing 10x the speed.

This new machine of course as powered by a computer, but at the time then the computer was pretty rudimentary, we're talking Pentium 60mhz being bleeding edge, and embedded solutions like in this copier much much more low power, so of course they used a compression mechanism.

I remember distinctly being told this copier had 32mb of ram, in a era of 4mb standard on the desktop, 32mb seemed like acres.  And the technician told us it could have up to 256mb in module updates... ooooo.....

I always trusted the copier, and assumed it did a 1:1 copy, oh how foolish I feel with this news in the headlines.

Of course a computer is doing this, so its using compression, but never ever did I think about this, nor did I ever think it would be a good idea for that compression software to CHANGE the figures on a copy!

I mean, that's the sole purpose of the hardware, to get form a to b the exact same image data, yet it does not?  And then I thought about the comments further, this software is READING, interpreting, the copy its making changing 6's to 8's.. that's not solely just image break up, that's a decision being made, that's OCR, that's bloody worrying.

I don't want machines reading my documents, I don't want the laser printer to refuse to print because my document is stupid, I don't want the machine to make any assumptions that it knows best, the phrase after all is WYSIWYG... What you see is what you get... I don't W Y S I N W Y G B W T M D F Y.... What you see is not what you get but what the machine decided for you.

The whole affair smacks of too technical a result being squashed into the machines to make them cheaper, and an utter and total lack of testing to any decent degree.  The fact that this problem was only spotted in 2013 begs the question whether this is a new innovation back firing, or a long standing issue as well!