Skip to content

Instantly share code, notes, and snippets.

@nikic
nikic / objects_arrays.md
Last active April 12, 2024 17:05
Post explaining why objects often use less memory than arrays (in PHP)

Why objects (usually) use less memory than arrays in PHP

This is just a small post in response to [this tweet][tweet] by Julien Pauli (who by the way is the release manager for PHP 5.5). In the tweet he claims that objects use more memory than arrays in PHP. Even though it can be like that, it's not true in most cases. (Note: This only applies to PHP 5.4 or newer.)

The reason why it's easy to assume that objects are larger than arrays is because objects can be seen as an array of properties and a bit of additional information (like the class it belongs to). And as array + additional info > array it obviously follows that objects are larger. The thing is that in most cases PHP can optimize the array part of it away. So how does that work?

The key here is that objects usually have a predefined set of keys, whereas arrays don't:

@bradmontgomery
bradmontgomery / count_words.py
Last active August 15, 2022 19:11
playing with python's `collections.Counter`
"""
Use a Counter to find the most common words in "The Wonderful Wizard of Oz" by
L. Frank Baum.
Available in (mostly) plain text at:
https://archive.org/stream/wonderfulwizardo00baumiala/wonderfulwizardo00baumiala_djvu.txt
Note: This code also counts the words in the header, so it's not a *realistic*
applicaton, but more of a demonstration of python's Counter.
@jonashansen229
jonashansen229 / class.database.php
Last active June 20, 2023 08:41
PHP OOP Database class using MySQLI and Singleton pattern. Only one instance of the class will be made, this requires less memory.
<?php
/*
* Mysql database class - only one connection alowed
*/
class Database {
private $_connection;
private static $_instance; //The single instance
private $_host = "HOSTt";
private $_username = "USERNAME";
private $_password = "PASSWORd";
@ttscoff
ttscoff / rtftomarkdown.rb
Created October 9, 2012 21:05
Convert RTF/DOC files to Markdown via Textutil
#!/usr/bin/ruby
=begin
Usage: rtftomarkdown.rb FILENAME.rtf
Uses textutil, available on Mac only (installed by default)
Outputs to STDOUT
Notes:
Links are replaced with Markdown references (duplicate links combined).
@almost
almost / glacier.py
Created August 21, 2012 17:59
Amazon Glacier from Python. There's now a branch for this, see here: https://github.com/almost/boto/tree/glacier
# Thomas Parslow http://almostobsolete.net
# Just a work in progress and adapted to what I need right now.
# It does uploads (via a file-like object that you write to) and
# I've started on downloads. Needs the development version of Boto from Github.
#
# Example:
#
# glacierconn = GlacierConnection(AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
# writer = GlacierWriter(glacierconn, GLACIER_VAULT)
# writer.write(somedata)
@j3tm0t0
j3tm0t0 / Upload2Glacier.ps1
Created August 21, 2012 09:11
Upload a file to Glacier PowerShell script version
Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\AWSSDK.dll"
$region="ap-northeast-1"
$accesskey="ACCESSKEY"
$secretkey="SECRETKEY"
$vaultname="myvault"
$description="some binary file"
$file="c:\hoge\target.bin"
$endpoint = [Amazon.RegionEndpoint]::GetBySystemName($region)
@Samuirai
Samuirai / captcha.md
Created June 14, 2012 20:59
G-WAN Captcha Decode

G-WAN is a new free web server. They seem to be very proud of it, or at least just want to make a lot of money. Well anyway, in almost every sentence they write, they claim that they are 20% cooler than anything else. It feels a bit arrogant. I have to admit, I don't know a lot about web servers, so I can't speak to how good they are.

However, then I saw their Captcha example. I also don't know much about machine learning algorithms, OCR, and stuff like that, but I do know how to read pixels. I also know how to compare values with python :P

demo

They say the following about their Captcha:

@jdan
jdan / wiki-scraper.rb
Created January 21, 2012 21:23
(Nokogiri) Tests the idea that the first link on each wikipedia article will eventually lead to philosophy
#!/usr/bin/env ruby
# wiki-scraper.rb by Jordan Scales
# http://jordanscales.com
# http://programthis.net
#
# Tests the idea that the first link on each wikipedia article
# will eventually lead to philosophy
#
# Usage:
@bentruyman
bentruyman / Custom.css
Created August 17, 2011 00:30
IR_Black Theme for Chrome Developer Tools
/**********************************************/
/*
/* IR_Black Skin by Ben Truyman - 2011
/*
/* Based on Todd Werth's IR_Black:
/* http://blog.toddwerth.com/entries/2
/*
/* Inspired by Darcy Clarke's blog post:
/* http://darcyclarke.me/design/skin-your-chrome-inspector/
/*
@jwage
jwage / SplClassLoader.php
Last active April 9, 2024 21:04
Add MIT license.
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,